机器人 救救瓜
Toggle navigation
Home
SCM-tool
Linux
Jenkins
SVN
other
About Me
Archives
Tags
shell 里awk 的用法
? shell ?
2021-01-20 17:03:25
1282
0
0
gua_l
? shell ?
打印全部列 ``` awk '{print $0}' ``` 打印指定列 ``` awk '{print $2,$4}' ``` 打印指定列并拼接字符串 ``` awk '{print $1,"hhah",$2}' awk '{print $1"hahah"$2}' ``` 按指定分隔符取列 ``` awk -F: '{ print $1 }' ``` 打印某一列及后面所有列的内容 ``` awk '{out=""; for(i=2;i<=NF;i++){out=out" "$i}; print out}' ``` 打印最后一列,最后一列的前一列,NF表示最后一列的列号 ``` awk '{print $NF, $(NF-1)}' ``` 打印指定一行,NR表示行号 ``` awk 'NR==6 {print $0}' ``` 打印第六行,以 :分隔开的第一列 ``` awk -F ":" 'NR==6 {print $1}' ``` 打印第4行以后的行 ``` git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR>4 {print $0}' 5 1f021028e22d92119fa5a0821273596e0c37be87-1f02102:1f02102 6 f36b0ad5a376edf485979b357af4ac1df87cc443-f36b0ad:f36b0ad 7 e3776955f8f3592051603d2299b93d3b55cbc52a-e377695:e377695 8 94c50a1fd810d5c1e62535fac8f6db3c01c4c476-94c50a1:94c50a1 git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR<4 {print $0}' git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR<=4 {print $0}' ``` **条件用法** 打印包含 Merge branch 的行 用/strings/ ``` git log --pretty=format:"%s" -n 15 |cat -n |awk '/Merge branch/ {print $0}' ``` 打印第一列为10 的 那一行的第一列和第二列 ``` awk '$1==10 {print $1 $2}' ``` 打印第二列为Merge 的 那一行的第一列和第二列 ``` awk '$2=="Merge" {print $1 $2}' ``` **函数用法** ``` # toupper() 转换大写 git log --pretty=format:"%H" -n 15 |awk '{print toupper($1)}' 098004D5D82D37D18B2909AEF53C8D3A22104030 46CBDB3641371BA4516F9E0ECCCB66B8304D8D6A # tolower() 转换小写 git log --pretty=format:"%H" -n 15 |awk '{print toupper($1)}'|awk '{print tolower($1)}' 098004d5d82d37d18b2909aef53c8d3a22104030 46cbdb3641371ba4516f9e0ecccb66b8304d8d6a ``` END前面的语句会对每一行都执行 ``` git log --pretty=format:"%H %h" -n 2 |awk '{print toupper($0)}'| awk '{string=$1"%%"$2} {print string}' 098004D5D82D37D18B2909AEF53C8D3A22104030%%098004D 46CBDB3641371BA4516F9E0ECCCB66B8304D8D6A%%46CBDB3 git log --pretty=format:"%H %h" -n 2 |awk '{print toupper($0)}'| awk '{string=$1"%%"$2} END {print string}' 46CBDB3641371BA4516F9E0ECCCB66B8304D8D6A%%46CBDB3 git log --pretty=format:"%H %h" -n 2 |awk '{print toupper($0)}'| awk '{string=$1"%%"$2}{print string} END {print string}' 098004D5D82D37D18B2909AEF53C8D3A22104030%%098004D 46CBDB3641371BA4516F9E0ECCCB66B8304D8D6A%%46CBDB3 46CBDB3641371BA4516F9E0ECCCB66B8304D8D6A%%46CBDB3 ## 标准计数 # 打印过程 seq 5|awk '{ total+=$1} {print "After add " $1,"total number is :" total } END {print total }' After add 1 total number is :1 After add 2 total number is :3 After add 3 total number is :6 After add 4 total number is :10 After add 5 total number is :15 15 # 只计总数 seq 5|awk '{ total+=$1} END {print total }' 15 ``` BEGIN END 示例中哪一段默认参数有效 下方的例子表示 BEGIN 后面接的第一个{}的语句里,\$1\$0 无值,NF NR 为0 ,BEGIN 第二个{}开始到END 之前的才是循环体,之间的\$X NF NR 会按实际生效读值,END 之后的{} 里读的是最后一行的值,所以想要循环计数,{total+=$1}必须放在循环体里 ``` seq 5|awk 'BEGIN {print "BEGIN LINE " $0,$1 "NF"NF "NR"NR} {total+=$1} {print $0$1 "NF"NF "NR"NR} {print "After add " $1,"total number is :" total } END {print "END LINE "total ,$0,$1 ,"NF"NF "NR"NR}' BEGIN LINE NF0NR0 11NF1NR1 After add 1 total number is :1 22NF1NR2 After add 2 total number is :3 33NF1NR3 After add 3 total number is :6 44NF1NR4 After add 4 total number is :10 55NF1NR5 After add 5 total number is :15 END LINE 15 5 5 NF1NR5 ```
Pre:
生成序列 seq 用法
Next:
使用gitlab API 与jq 筛选器 查找信息的例子
0
likes
1282
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.