点击(此处)折叠或打开
- [root@krlcgcms01 mytest]# cat test1 //测试文件
- 111 222
- 333 444
- 555 666
点击(此处)折叠或打开
- [root@krlcgcms01 mytest]# awk '{print $0}' test1 //awk 'BEGIN{RS="\n"}{print $0}' test1 这二个是一样的
- 111 222
- 333 444
- 555 666
点击(此处)折叠或打开
- [zhangy@localhost test]$ echo "111 222|333 444|555 666"|awk 'BEGIN{RS="|"}{print $0,RT}'
- 111 222 |
- 333 444 |
- 555 666
点击(此处)折叠或打开
- [zhangy@localhost test]$ echo "111 222a333 444b555 666"|awk 'BEGIN{RS="[a-z]+"}{print $1,RS,RT}'
- 111 [a-z]+ a
- 333 [a-z]+ b
- 555 [a-z]+
点击(此处)折叠或打开
- [zhangy@localhost test]$ cat -n test2
- 1 111 222
- 2
- 3 333 444
- 4 333 444
- 5
- 6
- 7 555 666
- [zhangy@localhost test]$ awk 'BEGIN{RS=""}{print $0}' test2
- 111 222
- 333 444
- 333 444
- 555 666
- [zhangy@localhost test]$ awk 'BEGIN{RS="";}{print "<",$0,">"}' test2 //这个例子看着比较明显
- < 111 222 >
- < 333 444 //这一行和下面一行,是一行
- 333 444 >
- < 555 666 >
点击(此处)折叠或打开
- [zhangy@localhost test]$ awk 'BEGIN{ORS="\n"}{print $0}' test1 //awk '{print $0}' test1二者是一样的
- 111 222
- 333 444
- 555 666
- [zhangy@localhost test]$ awk 'BEGIN{ORS="|"}{print $0}' test1
- 111 222|333 444|555 666|
点击(此处)折叠或打开
- [zhangy@localhost test]$ echo "111|222|333"|awk '{print $1}'
- 111|222|333
- [zhangy@localhost test]$ echo "111|222|333"|awk 'BEGIN{FS="|"}{print $1}'
- 111
点击(此处)折叠或打开
- [zhangy@localhost test]$ echo "111||222|333"|awk 'BEGIN{FS="[|]+"}{print $1}'
- 111
点击(此处)折叠或打开
- [zhangy@localhost test]$ echo "111|222|333"|awk 'BEGIN{FS=""}{NF++;print $0}'
- 1 1 1 | 2 2 2 | 3 3 3
点击(此处)折叠或打开
- [zhangy@localhost test]$ cat test1
- 111 222
- 333 444
- 555 666
- [zhangy@localhost test]$ awk 'BEGIN{RS="444";}{print $2,$3}' test1
- 222 333
- 666
点击(此处)折叠或打开
- [zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{print $1,$2}' test1
- 111|222
- 333|444
- 555|666
- [zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{print $1 OFS $2}' test1
- 111|222
- 333|444
- 555|666
- test1只有二列,如果100列,都写出来太麻烦了吧。
- [zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{print $0}' test1
- 111 222
- 333 444
- 555 666
- [zhangy@localhost test]$ awk 'BEGIN{OFS="|";}{NF=NF;print $0}' test1
- 111|222
- 333|444
- 555|666