强大的grep,sed和awk–用案例来讲解

准备工作:

先简单了解grep,sed和awk功能  

1) grep 显示匹配特定模式的内容

  • grep -v ‘boy’ test.txt 过滤掉test.txt文件的boy,显示其余内容
  • grep ‘boy’ test.txt 显示test.txt文件中,和boy匹配的内容

  • -E 同时过滤多个”a|b”

  • -i 不区分大小写

  • –color=auto 设置颜色

2)sed 取各种内容,以行为单位取内容

  • -n取消默认输出
  • p=print

  • d=delete 

3)awk 取列

  • -F 指定分割符 如对“I am a student” 以空格为分割符,其将被分为4列,awk里有参数可以去任意列
  • NF 表示当前行记录域或列的个数

  • NR 显示当前记录号或行号

  • $1第一列 $2第二列 $0整行 $NF 最后一列        

案例一:如何过滤出em1的ip地址

[zhaohuizhen@localhost Test]$ ifconfig em1
em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254
inet6 fe80::b283:feff:fed9:6a9a prefixlen 64 scopeid 0x20<link>
ether b0:83:fe:d9:6a:9a txqueuelen 1000 (Ethernet)
RX packets 13908772 bytes 4072069839 (3.7 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 982482 bytes 86260856 (82.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 40

步骤一

首先应该过滤出第二行inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254内容

方法一:grep命令

[zhaohuizhen@localhost Test]$ ifconfig em1 | grep 'inet '
 inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

  
方法二:用sed命令

[zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n '2p'
  inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

方法三:用awk命令

[zhaohuizhen@localhost Test]$ ifconfig em1 | awk NR==2

  inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

  
方法四:用head,tail命令

[zhaohuizhen@localhost Test]$ ifconfig em1 | head -2 | tail -1
  inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.254

步骤二

过滤出第二行后,在过滤出ip地址

方法一:用cut命令

[zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n '2p' | cut -c 14-25
   10.0.0.8

[zhaohuizhen@localhost Test]$ ifconfig em1 | grep 'inet ' | cut -d" " -f10
  10.0.0.8

方法二:用awk命令

[zhaohuizhen@localhost Test]$ ifconfig em1 | grep 'inet ' | awk -F '[ ]+' '{print $3}'
  10.0.0.8

用awk命令可以直接处理第二行,不用先将其过滤出来    

[zhaohuizhen@localhost Test]$ ifconfig em1 | awk -F '[ ]+' 'NR==2 {print $3}' 
  10.0.0.8

  
方法三:用sed命令

[zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n '/inet /p' | sed 's#^.*et ##g' | sed 's# net.*$##g'
  10.0.0.8

此处用到了正则表达式(见http://www.cnblogs.com/ZGreMount/p/7656365.html),匹配的目标前面的字符串一般以^.开头,代表以任意字符开头,结尾写上要匹配的字符前面的几个字符,     如”^.addr “就匹配” inet addr “,而处理的目标后的内容则是开头写上要匹配字符后几个字符,加上以.$。如,“ Bcast:.$”就匹配“ Bcast:10.0.0.254 Mask:255.255.255.”

注:sed小括号分组功能

sed ‘s/********/……./标签’ #斜线可以被其它字符替换

前两条斜线中间部分内容********,可以使用正则表达式,后两条斜线中间内容…….不能使用正则表达式。

()是分组,在前面部分使用()括起来的内容,在后面部分可以使用\1调用前面括号内内容。

如果有多个括号,那么依次是\1,\2,\3,以此类推。

例如,直接取em1ip地址,不先过滤出第二行

[zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n 's#^.*inet \(.*\) net.*$#\1#gp'
  10.0.0.8

    
直接取出ip地址和子网掩码

[zhaohuizhen@localhost Test]$ ifconfig em1 | sed -n 's#^.*inet \(.*\) n.*k \(.*\) bro.*$#\1 \2#gp'
  10.0.0.8 255.255.255.0

案例二:输出文件a对应权限664

[zhaohuizhen@localhost Test]$ ll a 
   -rw-rw-r--. 1 zhaohuizhen zhaohuizhen 98 Oct 12 20:24 a

    
方法一:用awk命令

[zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'|tr rwx- 4210|awk -F "" '{print $2+$3+$4 $5+$6+$7 $8+$9+$10}'
  664

    
解析:

1)ll a 长格式显示文件a    

[zhaohuizhen@localhost Test]$ ll a
  -rw-rw-r--. 1 zhaohuizhen zhaohuizhen 98 Oct 12 20:24 a

      
2)用awk命令,以空格为分隔符,取出第一列

[zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'
  -rw-rw-r--.

      
3)用tr命令将rwx- 替换为4210

[zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'|tr rwx- 4210
  0420420400.

      
4)用awk将上面的结果分割,然后相加得出结果

[zhaohuizhen@localhost Test]$ ll a | awk '{print $1}'|tr rwx- 4210|awk -F "" '{print $2+$3+$4 $5+$6+$7 $8+$9+$10}'
  664

方法二:用stat命令

[zhaohuizhen@localhost Test]$ stat a
File: ‘a’
Size: 98 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 203491 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1002/zhaohuizhen) Gid: ( 1002/zhaohuizhen)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2017-10-14 09:20:34.337529787 +0800
Modify: 2017-10-12 20:24:27.512609708 +0800
Change: 2017-10-12 20:24:27.536609708 +0800
Birth: -

    
1)命令stat a结果包含文件a对应权限644,可以用前面的方法直接过滤出来

[zhaohuizhen@localhost Test]$ stat a | awk -F '[(/]' 'NR==4 {print $2}' 
  0664

    
2)stat命令包含需要结果,考虑stat命令是否有参数可以直接获得我们需要的结果

[zhaohuizhen@localhost Test]$ stat -c %a a
  664

案例三:输出文件a内容,不带空行,文件a内容如下:

[zhaohuizhen@localhost Test]$ cat a
"hello,this is a test"
I am a studeng My QQ is 1534612574

computer

book

river
tree

man
computer

book
river
tree
man

  
方法一:grep命令

[zhaohuizhen@localhost Test]$ grep -v '^$' a
"hello,this is a test"
I am a studeng My QQ is 1534612574
computer
book
river
tree
man
computer
book
river
tree
man

    
注释:-v 即排除;^$,开头和结尾间没有任何东西,即空行

方法二:用sed命令

[zhaohuizhen@localhost Test]$ sed '/^$/d' a
"hello,this is a test"
I am a studeng My QQ is 1534612574
computer
book
river
tree
man
computer
book
river
tree
man

注释:^$代表空行,d即delete

方法三:用awk命令

[zhaohuizhen@localhost Test]$ awk /[^$]/ a
"hello,this is a test"
I am a studeng My QQ is 1534612574
computer
book
river
tree
man
computer
book
river
tree
man

    
注释:^$代表空行,放在[]中代表非,即不匹配空行。

发布于:2019-11-06 08:14:55