教你如何使用 awk 處理文本內(nèi)容


點擊「閱讀原文」查看良許原創(chuàng)精品視頻。
點擊「閱讀原文」查看良許原創(chuàng)精品視頻。
[root@localhost?~]#?cat?<>?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技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機,樹莓派,等等。在公眾號內(nèi)回復(fù)「1024」,即可免費獲取?。?/span>
評論
圖片
表情
