Introduction
- In fact, a programming language that supports conditional judgment, arrays, loops and other functions, and grep, sed is called the Linux Three Musketeers
- The reason why it is called AWK is because it takes the initials of the Family Name of its three founders Alfred Aho, Peter Weinberger and Brian Kernighan
Application scenarios of awk
- Usually extract the columns of the data
awk 'condition {execute action}' file name or awk 'condition 1 {execute action} condition 2 {execute action} ...' file name or awk [option] 'condition 1
{execute action} condition 2 {execute action} ...' file name
Key points
- printf : Formatted output without automatic line breaks.
%ns:字符串型,n代表有多少个字符.
%ni:整型,n代表输出几个数字.
%.nf:浮点型,n代表的是小数点后有多少个小数.
- print : Print out the content, which will automatically wrap by default.
- \t : tab character
- \n : newline character
lelontar@bogon ~/Downloads printf "%s %s %s\n" 1231 2321 3441 4551 5661 6771
1231 2321 3441
4551 5661 6771
- Basic usage of awk
lelontar@bogon ~/Downloads df -h | grep '/dev/disk1s1s1' | awk '{printf "/dev/disk1s1s1使用空间是:"} {print $5}'
/dev/disk1s1s1使用空间是:5%
- 「 1... 1... 1... n represents the number of columns" "$0 represents the entire row"
lelontar@bogon ~/Downloads df -h | grep '/dev/disk1s1s1' | awk '{print $5}'
5%
lelontar@bogon ~/Downloads df -h | grep '/dev/disk1s1s1' | awk '{print $0}'
/dev/disk1s1s1 466Gi 14Gi 315Gi 5% 568975 4881883905 0% /
- "-F": specify the delimiter
lelontar@bogon ~/Downloads cat passwd.txt|awk -F":" '{print $2}'
*
*
*
*
*
*
*
*
*
*
- NR : line number
lelontar@bogon ~/Downloads cat passwd.txt| awk '(NR>=2&&NR<=4){print $0}' ✔ 11:22:28
_logd:*:272:272:Log Daemon:/var/db/diagnostics:/usr/bin/false
_appinstalld:*:273:273:App Install Daemon:/var/db/appinstalld:/usr/bin/false
_installcoordinationd:*:274:274:Install Coordination Daemon:/var/db/installcoordinationd:/usr/bin/false
lelontar@bogon ~/Downloads df -h |awk '(NR>=2 && NR<=4){print $3}'
14Gi
189Ki
4.0Gi
- BEGIN: Start execution before reading all line content, often used to modify the value of built-in variables
- Define separator when FS:BEGIN
lelontar@bogon ~/Downloads cat passwd.txt | awk 'BEGIN {FS=":"} {print $1}'
_diskimagesiod
_logd
_appinstalld
_installcoordinationd
_demod
_rmd
_fud
_knowledgegraphd
_coreml
_oahd
- END : execute at the end
lelontar@bogon ~/Downloads cat passwd.txt | awk 'BEGIN {FS=":"} {print $1} END{print $3}'
_diskimagesiod
_logd
_appinstalld
_installcoordinationd
_demod
_rmd
_fud
_knowledgegraphd
_coreml
_oahd
441
- use -F ""
grep "SpeedFeedMatchPicServiceImpl jsonResultData end query:" speedDetail.log |awk -F"," '{print $1}'|less