linux shell中利用awk获取关键字符的用法,实例说明。

来源:互联网 发布:淘宝饰品店店铺介绍 编辑:程序博客网 时间:2024/04/30 19:23

1busybox ifconfig wlan0

 

wlan0     Link encap:Ethernet  HWaddr 3C:CF:5B:84:15:90  

          inet addr:192.168.1.115  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::3ecf:5bff:fe84:1590/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:4224 errors:0 dropped:0 overruns:0 frame:0

          TX packets:1016 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:525489 (513.1 KiB)  TX bytes:67242 (65.6 KiB)

 

2

awk 默认以空格作为分隔符

"addr:"为分间隔。在“addr:”之前的数据为"$1","addr:"之后的数据为“$2

 

命令:busybox ifconfig wlan0 | busybox awk -F'addr:' '{print $1}'

 

wlan0     Link encap:Ethernet  HWaddr 3C:CF:5B:84:15:90  

          inet

          inet6

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:4243 errors:0 dropped:0 overruns:0 frame:0

          TX packets:1016 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:527614 (515.2 KiB)  TX bytes:67242 (65.6 KiB)

 

命令:busybox ifconfig wlan0 | busybox awk -F'addr:' '{print $2}'

 

192.168.1.115  Bcast:192.168.1.255  Mask:255.255.255.0

 fe80::3ecf:5bff:fe84:1590/64 Scope:Link

 

3、将"addr:" "Bcast:"都作为分隔符,看效果

 

命令:busybox ifconfig wlan0 | busybox awk -F'addr:|Bcast:' '{print $2}'

 

192.168.1.115  

 fe80::3ecf:5bff:fe84:1590/64 Scope:Link

 

命令:busybox ifconfig wlan0 | busybox awk -F'addr:|Bcast:' '{print $3}'

 

192.168.1.255  Mask:255.255.255.0

 

4、打印出关键字的所在行的信息,默认以空格为分隔符

 

命令:busybox ifconfig wlan0 | busybox awk '/Mask/{print $1}'

 

inet

 

命令:busybox ifconfig wlan0 | busybox awk '/Bcast/{print $1}'

 

inet

 

命令:busybox ifconfig wlan0 | busybox awk '/Mask/{print $2}'

 

addr:192.168.1.115

 

命令:busybox ifconfig wlan0 | busybox awk '/Mask/{print $3}'

 

Bcast:192.168.1.255

 

命令:busybox ifconfig wlan0 | busybox awk -F'addr:' '/Mask/{print $2}'

 

192.168.1.115  Bcast:192.168.1.255  Mask:255.255.255.0

 

命令:busybox ifconfig wlan0 | busybox awk -F'addr:|Bcast' '/Mask/{print $2}'

 

192.168.1.115 (空格)  

 

5、可以通过tr命令删除某些字符

 

命令:lsmod

 

dwc_otg 281370 0 - Live 0x0000000000000000

aml_thermal 18163 0 - Live 0x0000000000000000 (O)

mali 195296 10 - Live 0x0000000000000000 (O)

aml_nftl_dev 82380 0 - Live 0x0000000000000000 (PO)

 

命令:lsmod | busybox tr -d "aml"

 

dwc_otg 281370 0 - Live 0x0000000000000000

_ther 18163 0 - Live 0x0000000000000000 (O)

i 195296 10 - Live 0x0000000000000000 (O)

_nft_dev 82380 0 - Live 0x0000000000000000 (PO)

 

'a','m','l'字符都给删除了

 

6busybox ifconfig wlan0 | busybox awk -F 'addr:|Bcast' '/Mask/{print $2}' | busybox tr -d " "

得到不含空格的IP地址:192.168.1.115

0 0
原创粉丝点击