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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM