William126

50个常用的Linux命令(三)awk

[root@localhost cee]# echo -e "line1\nline2"|awk '{ print }'
line1
line2
[root@localhost cee]# echo -e "line1\nline2"|awk 'BEGIN{ print "START"} { print }'
START
line1
line2
[root@localhost cee]# echo -e "line1\nline2"|awk 'BEGIN{ print "START"} { print } END{ print "END" }'
START
line1
line2
END

[root@localhost cee]# echo | awk '{ var1="v1";var2="v2";var3="v3"; print var1,var2,var3}'
v1 v2 v3
[root@localhost cee]# echo | awk '{ var1="v1";var2="v2";var3="v3"; print var1"-"var2"-"var3}'
v1-v2-v3

[root@localhost cee]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7"
line1 f2 f3
line2 f4 f5
line3 f6 f7
[root@localhost cee]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7"| awk '{ print "Line no:"NR",No of filelds:"NF, "$0="$0, "$1="$1,"$2="$2,"$3="$3 }'
Line no:1,No of filelds:3 $0=line1 f2 f3 $1=line1 $2=f2 $3=f3
Line no:2,No of filelds:3 $0=line2 f4 f5 $1=line2 $2=f4 $3=f5
Line no:3,No of filelds:3 $0=line3 f6 f7 $1=line3 $2=f6 $3=f7


[root@localhost cee]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | awk '{ print $3,$2}'
f3 f2
f5 f4
f7 f6
[root@localhost cee]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | awk '{ print $0}'
line1 f2 f3
line2 f4 f5
line3 f6 f7
[root@localhost cee]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7"
line1 f2 f3
line2 f4 f5
line3 f6 f7
echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | awk 'END{ print NR }'
3

[root@localhost cee]# seq 5
1
2
3
4
5
[root@localhost cee]# seq 5 | awk 'BEGIN{ sum=0; print "Summation:" } { print $1"+"; sum+=$1 } END{ print "=="; print sum }'
Summation:
1+
2+
3+
4+
5+
==
15

posted on 2019-02-18 17:34  William126  阅读(109)  评论(0编辑  收藏  举报

导航