教你如何使用 awk 处理文本内容
良许Linux
共 2456字,需浏览 5分钟
·
2020-10-21 07:51
点击「阅读原文」查看良许原创精品视频。
点击「阅读原文」查看良许原创精品视频。
[root@localhost ~]# cat << EOF >> content.txt
hitesh engineer sales 30000
jayesh director account 25000
vyom manager purchase 20000
bhavesh engineer sales 30000
rajesh directory sales 40000
niraj clerk account 20000
jay peon purchase 23000
deep clerk sales 20000
EOF
NF
,从文件中删除所有空白行。例如,删除文件content.txt中的所有空白行。[root@localhost ~]# awk NF content.txt
[root@localhost ~]# awk '{ sub(/^[ \t]+/, ""); print }' content.txt
[root@localhost ~]# awk '{ sub(/[ \t]+$/, ""); print }' content.txt
[root@localhost ~]# awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }' content.txt
[root@localhost ~]# awk '{ b[i++] = $0 } END { for (j=i-1; j>=0;) print b[j--] }' content.txt
[root@localhost ~]# awk 'NF { $0=++a ": " $0 }; { print }' content.txt
[root@localhost ~]# awk '/engineer/{n++}; END {print n+0}' content.txt
[root@localhost ~]# awk '/engineer/' content.txt
[root@localhost ~]# awk '!/jayesh/' content.txt
[root@localhost ~]# awk '{gsub(/engineer/,"doctor")}{print}' content.txt
[root@localhost ~]# awk '{gsub(/jayesh|hitesh|bhavesh/ , "mahesh"); print}' content.txt
[root@localhost ~]# df -h | awk '{printf("%-24s \t %-6s \t %-4s \n",$1,$4,$5)}'
[root@localhost ~]# netstat -ntu|awk '{print $5}'|cut -d: -f1|awk '/[0-9]/'|sort|uniq -c|sort -n
推荐阅读:
5T技术资源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,单片机,树莓派,等等。在公众号内回复「1024」,即可免费获取!!
评论