页次: 1
awk 是进行字段处理的。
一个文件有一行或者多行组成的。 行和行的之间的就是 RS 默认是 \n
ORS 是在输出的时候, 到“下一个行”的时候用什么字符,进行分割。
同理 FS是 字段之间的分割, 也就是一行一行的分割。
OFS 就是输出下一个字段的时候, 用什么字符。
例子
[root@qd ~]# cat txt this is one this is two this is trhee
[root@qd ~]# awk 'BEGIN{RS=" "}{print $0}' txt this is one this is two this is trhee
注意最后的空格处理。 这里行分隔符用了 空格 (一个)
[root@qd ~]# awk 'BEGIN{RS=" ";ORS="#"}{print $0}' txt this#is#one this#is#two this#is#trhee
如何想变成一行可以用
awk 'BEGIN{RS="\n";ORS="#"}{print $0}' txt this is one#this is two#this is trhee##
这个把\n 变成了 #
#[root@qd ~]# awk 'BEGIN{RS="";ORS="\n"}{print $0}' txt this is one this is two this is trhee
这个可以看出有删除空行的功能呢
FS 是域分隔符, 默认是空格,
[root@qd ~]# echo "111 222 333"|awk 'BEGIN{FS=" "}{print $1}' 111 [root@qd ~]# echo "111 222 333"|awk 'BEGIN{FS="a"}{print $1}' 111 222 333 [root@qd ~]# echo "111 222 333"|awk 'BEGIN{FS=""}{print $1}' 1 [root@qd ~]# echo "111 222 333"|awk 'BEGIN{FS=" "}{print $1}' 111 [root@qd ~]# echo "111 222 333"|awk 'BEGIN{FS=" ";OFS="#"}{print $1}' 111 [root@qd ~]# echo "111 222 333"|awk 'BEGIN{FS=" ";OFS="#"}{print $0}' 111 222 333 [root@qd ~]# echo "111 222 333"|awk 'BEGIN{FS=" ";OFS="#"}{print $1 OFS $2}' 111#222
ipbbs.net
离线
页次: 1