cut及awk简单使用

1.1 cut:从文本中提取一段文字并输出

用法:cut -d “:” -f2 文件

-c   以字符为单位进行分割

-d   分隔符,后面用引号引住分隔符

-f   与-d 连用,指定显示那个区域

实例1-1  

[root@jz ~]# cat a.txt                          

12 34 56 78 9

[root@jz ~]# cut -c 1-5 a.txt              《《==截取a.txt文件第1到5个字符

12 34

[root@jz ~]# cut -d " " -f 2 a.txt         《《==以空格为分隔符,取第二列内容。

34

1.2 awk 擅长取列

用法:awk [options]  ‘{print NR,$0}’ file

-F   指定字段分隔符

NR   表示行号

$0   表示这一行的内容

$1   数字 某一列

$NF  最后一列

实例1-2  

[root@jz ~]# ifconfig eth0                 《《==创造环境

eth0      Link encap:Ethernet  HWaddr 00:0C:29:E8:64:67 

          inet addr:10.0.0.7  Bcast:10.0.0.255  Mask:255.255.255.0

          inet6 addr: fd15:4ba5:5a2b:1008:20c:29ff:fee8:6467/64 Scope:Global

          inet6 addr: fe80::20c:29ff:fee8:6467/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:213540025 (203.6 MiB)  TX bytes:6069350 (5.7 MiB)

过滤出HWaddr字符所在地行

[root@jz ~]# ifconfig eth0|awk '/HWaddr/'

eth0      Link encap:Ethernet  HWaddr 00:0C:29:E8:64:67 

过滤出第3行到第5行,并打印行号

[root@jz ~]# ifconfig eth0|awk 'NR==3,NR==5{print NR,$0}'

3           inet6 addr: fd15:4ba5:5a2b:1008:20c:29ff:fee8:6467/64 Scope:Global

4           inet6 addr: fe80::20c:29ff:fee8:6467/64 Scope:Link

5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

方法2:

[root@jz ~]# ifconfig eth0|awk 'NR>2&&NR<6{print NR,$0}'

3           inet6 addr: fd15:4ba5:5a2b:1008:20c:29ff:fee8:6467/64 Scope:Global

4           inet6 addr: fe80::20c:29ff:fee8:6467/64 Scope:Link

5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

打印全文行号

[root@jz ~]# ifconfig eth0|awk '{print NR,$0}'

1 eth0      Link encap:Ethernet  HWaddr 00:0C:29:E8:64:67 

2           inet addr:10.0.0.7  Bcast:10.0.0.255  Mask:255.255.255.0

3           inet6 addr: fd15:4ba5:5a2b:1008:20c:29ff:fee8:6467/64 Scope:Global

4           inet6 addr: fe80::20c:29ff:fee8:6467/64 Scope:Link

5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

6           RX packets:215517 errors:0 dropped:0 overruns:0 frame:0

7           TX packets:54325 errors:0 dropped:0 overruns:0 carrier:0

8           collisions:0 txqueuelen:1000

9           RX bytes:213599924 (203.7 MiB)  TX bytes:6108480 (5.8 MiB)

10

取出ip地址:

[root@jz ~]# ifconfig eth0|awk -F '[: ]+' '{print $4}'|awk 'NR==2'

10.0.0.7

[root@jz ~]# ifconfig eth0|awk -F '[: ]+' 'NR==2{print $4}'

10.0.0.7

[root@jz ~]# ifconfig eth0|awk -F '[: ]+' 'NR>1&&NR<3{print $4}'

10.0.0.7

[root@jz ~]# ifconfig eth0|grep '10.0.0.7'|awk -F '[ :]+' '{print $4}'

10.0.0.7

[root@jz ~]# ifconfig eth0|sed -n 2p|awk -F '[ :]+' '{print $4}'

10.0.0.7   

[root@jz ~]# ifconfig eth0|sed -nr '2s#^.*r:(.*) B.*$#1#gp'

10.0.0.7

[root@jz ~]# ifconfig eth0|awk 'NR==2'|cut -d ':' -f 2|cut -d ' ' -f 1

10.0.0.7

[root@jz ~]# ifconfig eth0|awk '/inet addr.*/'|awk -F '[: ]+' '{print $4}'

10.0.0.7

原文地址:https://www.cnblogs.com/jazzxs/p/9177883.html