awk!

            awk is a powerful editing tool that is more powerful than sed functionality can be achieved in the absence of interaction of the situation
rather complex text manipulation.

1.awk syntax

     awk [options] 'print $ 1' filename 

Options

    -F specify the delimiter

2, awk built-in variable
          · FS: Specify each line of text in the field separator, a space or a default tab stops
          · NF: number of fields currently processed row (number of columns)
          the ordinal number of the current row processing: · NR (number of rows)
          * $ 0: the entire contents of the line currently processed row
         · $ n: n-th currently processed field (n-th column)

          print: Printing
 3, use of awk
during use may be logical operators &&, means "and" || means "or"! It indicates "inactive"; also
to perform simple mathematical operations, such as +, -, *, /,%, ^ respectively add, subtract, multiply, divide, modulo, involution

4, awk based spaces and tabs, each row is divided into several fields, washed successively with $ 1, $ 2, $ 3 represents the first field, second field, third field and the like.

=======================================================================

[root@localhost ~]# cat awk.txt
abcgsg
banner
age
food
feet
more

010-123456
0791-1235677
ip 192.168.200.1
ip 192.168.200.111
ip 192.168.200.11
aBggfH
Bggsgs
gaglag1

Printing paper 

[the root @ localhost ~] # awk ' {} Print ' awk.txt Print print showing abcgsg Banner Age Food feet ' More 010 - 123456 0791 - 1,235,677 IP 192.168 . 200.1 IP 192.168 . 200.111 IP 192.168 . 200.11 aBggfH Bggsgs gaglag1
Content output line 1-3 of 

[the root @ localhost ~] # awk ' (NR> = 1) && (NR <=. 3)} {Print ' awk.txt line represents NR abcgsg Banner Age

output line and three 1

 [root@localhost ~]# awk 'NR==1||NR==3{print}' awk.txt
  abcgsg
  age

 
All base row output 

[root @ localhost ~] # awk ' (NR 2%). 1 == {} Print ' awk.txt% indicates modulo abcgsg Age feet ' 0791 - 1235677 IP 192.168 . 200.111 aBggfH gaglag1 output all even lines [root @ localhost
~] # awk ' (NR% 2) == 0} {Print ' awk.txt Banner Food More 010 - 123456 IP 192.168 . 200.1 IP 192.168 . 200.11 Bggsgs
The output of the first letter is capitalized


[root@localhost ~]# awk '/^[A-Z]/{print}' awk.txt Bggsgs
Mem: remaining much memory 

[the root @ localhost ~ ] # Free Total Used Free Shared BUFF / Cache Available Mem: 995 896 590 816 63260 8248 341 820 195436 Swap: 2,097,148 27648 2.0695 million [the root @ localhost ~] # Free | awk ' / Mem: / { ($. 3/2 * $ 100) "%" Print int} ' $. 3 shows a third column, the second column represents $ 2 59 %

filtered free total capacity

 [root@localhost ~]# free | grep 'Mem:' | awk '{print $2}'
 995896

过滤IP地址

[root@localhost ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.200.115 netmask 255.255.255.0 broadcast 192.168.200.255 inet6 fe80::656c:1296:c062:6af7 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c3:ed:86 txqueuelen 1000 (Ethernet) RX packets 4184 bytes 1545149 (1.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2329 bytes 257278 (251.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# ifconfig ens33 | awk -F '[ :]+' 'NR==2 {print $3}' 192.168.200.115
1 , extracts the / etc / the passwd file which, UID is less than the user name 1000 of the information and the UID 

[the root @ server0 ~] # awk -F: ' $. 3 <1000 {Print $ 1, $. 3} ' / etc / the passwd 



2 , extracted the / etc / passwd file which, environment shell "/ bin / the bash" shell user information has information 

[server0 the root @ ~] # awk -F: ' $ of NF == "/ bin / bash". 1 {Print $, $ of NF} ' / etc / the passwd 



. 3 , extract the / etc / the passwd file among the user name of the first row to the second row 5 information 

[the root @ server0 ~] # awk -F: ' (NR> =. 1 && NR <= 5 ). 1} {Print $ ' / etc / the passwd 



. 4 , the print / et / the passwd odd rows 

[server0 the root @ ~] # awk -F: '{IF (! NR% 2 = 0) Print $ 0} ' / etc / the passwd 



. 5 , printing UID is not equal to the GID username 

[the root @ server0 ~] # awk -F: ' {IF Print $. 1 ($. 3 = $. 4!) } ' / etc / the passwd
 

. 6 .awk extracted user log ip address 

[fengxiaoli41 the root @ ~] # WHO | tail - . 1 | awk -F " [()] + "  ' {}. 5 Print $ ' 

192.168 . 10.1

 

Filter / etc / passwd username of 

[the root @ localhost ~] # awk -F ' : ' ' {}. 1 Print $ ' / etc / passwd

root
bin
daemon
adm
lp
sync

....

......

 

Guess you like

Origin www.cnblogs.com/cxm123123form/p/11456989.html
awk