head -n2 test.txt|awk -F ‘:‘ ‘{print $1}‘ //文件的前两行,以:分隔,打印第一段 head -n2 test.txt|awk -F ‘:‘ ‘{print $0}‘ //文件的前两行,以:分隔,打印所有的内容($N就是第N段,0就是所有字段),输出所有字段时可以省略-F awk -F ‘:‘ ‘{print $1"#"$2"#"$3"#"$4}‘ test.txt //以:分隔,取前四段,之间用#分隔 -F选项省略时,默认以空格或者空白字符为分隔符 awk -F ‘:‘ ‘{print $1,$2,$3,$4}‘ test.txt //以:分隔,输出前四段 awk ‘/oo/‘ test.txt //输出包含oo的行 awk -F ‘:‘ ‘$1 ~/oo/‘ test.txt //以:分隔,第一段包含oo的行 awk -F ‘:‘ ‘/root/ {print $1,$3} /test/ {print $2,$4}‘ test.txt //以:分隔,包含root的行输出第1,3段,包含test的输出第2,4段 awk -F ‘:‘ ‘$3=="0"‘ /etc/passwd //以:分隔,第三段是“0” 的行 awk -F ‘:‘ ‘$3>="500"‘ /etc/passwd //以:分隔,第三段大于字符500 的行 (数字加上双引号就代表字符而非数字) awk -F ‘:‘ ‘$3>=500‘ /etc/passwd //以:分隔,第三段大于500 的行 awk -F ‘:‘ ‘$7!="/sbin/nologin"‘ /etc/passwd //以:分隔,第7段不等于/sbin/nologin 的行
Awk-f ': ' $3<$4 '/etc/passwd//with: Delimited, 3rd segment less than fourth line awk-f ': ' $3> ' 5 ' && $3< ' 7 '/etc/passwd By: Delimited, 3rd paragraph greater than character 5 and less than the character 7 of the line Awk-f ': ' $3>1000 | | $7== "/bin/bash" '/etc/passwd//with: Delimited, 3rd =1000 or seventh =/bin/bash rows head-5/etc/passwd |awk-f ': ' {ofs= ' # '} {print $ , $3,$4} '///First 5 lines: Delimited, take 1,3,4 segment, separated by # awk-f ': ' {ofs= ' # '} {if ($3>1000) {print $1,$2,$3,$4}} '/etc/passwd//Will delimiter : Replace with #, the third paragraph is greater than 1000, then output 1,2,3,4 segment awk-f ': ' {ofs= ' # '} $3>1000 {print $1,$2,$3,$4}} '/etc/passwd//delimiter: Replace with #, third segment greater than 10 00, the output 1,2,3,4 segment HEAD-N3/ETC/PASSWD | Awk-f ': ' {print NF} '//first three lines with: delimited, with several segments of HEAD-N3/ETC/PASSWD | Awk-f ': ' {print NR} '//first three lines with: delimited, with several lines (plus line number) awk ' nr>40 '/etc/passwd//Fetch line number greater than 40 awk-f ': ' Nr<2 0 && $ ~/roo/'/etc/passwd//To: delimited, first 19 rows of Head-n 3/etc/passwd |awk-f ': ' "$1=" "root"//to: Delimited , the first paragraph is assigned the root awk-f ': ' {(tot=tot+$3)}; END {print tot} '/etc/passwd//with: Delimited, the third segment of each line is added awk-f ': '' {if ($1== "root") {print $}} '/etc/passwd//To: Separate, if the first paragraph is =root, all segments are output
Linux Learning Notes (28) awk