• head -n2 test.txt|awk -F ':' '{print $1}'
Print the first paragraph of the first two lines of test.txt;
Description: -F is followed by a delimiter; $1 represents the first paragraph; {} curly brackets enclose the print range;
• head -n2 test.txt|awk -F ':' '{print $0}'
Description: $0 prints all segments;
• awk -F ':' '{print $1"#"$2"#"$3"#"$4}'
Use: to print out the first paragraph, the second paragraph, the third paragraph, and the fourth paragraph as the delimiter, and # as the delimiter
illustrate:
• awk '/oo/' test.txt
matches a character or string; matches a line containing oo;
• awk -F ':' '$1 ~/oo/' test.txt
Match against a segment; match the line containing oo in the first segment;
• awk -F ':' '/root/ {print $1,$3} /test/ {print $1,$3}' test.txt
awk -F ':' '/root/ {print $1,$3} ; /test/ {print $1,$3}' test.txt
Multiple matches; print the first and third paragraphs containing root, and print the first and third paragraphs containing test;
Conditional operator:
== is equal to
> greater than
< less than
!= does not equal
>= greater than or equal to
<= less than or equal to
Note: When comparing numbers, you cannot add quotation marks. After adding quotation marks, the comparison is not based on the size of the number, but based on the ASCII code.
• awk -F ':' '$3==0' /etc/passwd
# root:x:0:0:root:/root:/bin/bash
Line with 0 in the third segment
• awk -F ':' '$3>=500' /etc/passwd
#polkitd:x:999:998:User for polkitd:/:/sbin/nologin
#gavin:x:1000:1000::/home/gavin:/bin/bash
#zhang:x:1001:1001::/home/zhang:/bin/bash
#user:x:1002:1002::/home/user:/bin/bash
Lines with the third paragraph greater than or equal to 500
• awk -F ':' '$7!="/sbin/nologin"' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
gavin:x:1000:1000::/home/gavin:/bin/bash
zhang:x:1001:1001::/home/zhang:/bin/bash
user:x:1002:1002::/home/user:/bin/bash
paragraph 7 is not a line of /sbin/nologin;
• awk -F ':' '$3<$4' /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
matches lines where $3 is less than $4;
• awk -F ':' '$3>"5" && $3<"7"' /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
After the numbers are quoted, the matching rules are compared according to the ASCII code
[root@gavin ~]# awk -F':' '$3>5 && $3<7' /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Compare numbers without quotation marks;
• awk -F ':' '$3>1000 || $7=="/bin/bash"' /etc/passwd
root:x:0:0:root:/root:/bin/bash
gavin:x:1000:1000::/home/gavin:/bin/bash
zhang:x:1001:1001::/home/zhang:/bin/bash
user:x:1002:1002::/home/user:/bin/bash
Match the line where the third paragraph is greater than 1000 or the seventh paragraph is /bin/bash;
• head -5 /etc/passwd |awk -F ':' '{OFS="#"} {print $1,$3,$4}'
root#0#0
bin#1#1
daemon#2#2
adm#3#4
lp#4#7
OFS specifies the separator between print segments;
• awk -F ':' '{OFS="#"} {if ($3>1000) {print $1,$2,$3,$4}}' /etc/passwd
zhang#1001#/bin/bash
user#1002#/bin/bash
If the third paragraph is greater than 1000, print the first four paragraphs, and use # as a delimiter;
[root@gavin ~]# awk -F':' '{OFS="#"} $3>1000 {print $1,$2,$3,$4}' /etc/passwd
zhang#x#1001#1001
user#x#1002#1002
• head -n3 /etc/passwd | awk -F ':' '{print NF}'
7
7
7
NF means there are several paragraphs; $NF means the last paragraph, if NF is 7, it is $7;
[root@gavin ~]# head -n3 /etc/passwd | awk -F ':' '{print $NF}'
/bin/bash
/sbin/nologin
/sbin/nologin
• head -n3 /etc/passwd | awk -F ':' '{print NR}'
1
2
3
NR: Indicates the line number; $NR indicates the number of lines to be printed;
[root@gavin ~]# head -n3 /etc/passwd | awk -F ':' '{print $NR}'
root
x
2
• awk 'NR>40' /etc/passwd
Print lines with line numbers greater than 40
• awk -F ':' 'NR<20 && $1 ~ /roo/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
Match lines less than line number less than 20 and containing roo;
• head -n 3 /etc/passwd |awk -F ':' '$1="root"'
root x 0 0 root /root /bin/bash
root x 1 1 bin /bin /sbin/nologin
root x 2 2 daemon /sbin /sbin/nologin
Change the first paragraph to root;
• awk -F ':' '{(all = all + $ 3)}; END {print all} '/ etc / passwd
4647
Calculate the sum of the third paragraph;
• awk -F ':' '{if ($1=="root") {print $0}}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
print the line where the first paragraph is equal to root;