grep線上環境精典案例後續

請執行命令取出 linux 中 eth0 的 IP 地址(請用 cut,有能力者也可分別用 awk,sed 命令答)。

自己的方法:

[root@nginx_back ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:21:B6:B1  

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe21:b6b1/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000 

          RX bytes:9020682 (8.6 MiB)  TX bytes:11377482 (10.8 MiB)


[root@nginx_back ~]# 

[root@nginx_back ~]# man -cut

man:無效選項 -- u

Cannot open the message catalog "man" for locale "zh_CN.UTF-8"

(NLSPATH="/usr/share/locale/%l/LC_MESSAGES/%N")


man, version 1.6f


usage: man [-adfhktwW] [section] [-M path] [-P pager] [-S list]

        [-m system] [-p string] name ...


  a : find all matching entries

  c : do not use cat file

  d : print gobs of debugging information

  D : as for -d, but also display the pages

  f : same as whatis(1)

  h : print this help message

  k : same as apropos(1)

  K : search for a string in all pages

  t : use troff to format pages for printing

  w : print location of man page(s) that would be displayed

      (if no name given: print directories that would be searched)

  W : as for -w, but display filenames only


  C file   : use `file' as configuration file

  M path   : set search path for manual pages to `path'

  P pager  : use program `pager' to display pages

  S list   : colon separated section list

  m system : search for alternate system's man pages

  p string : string tells which preprocessors to run

               e - [n]eqn(1)   p - pic(1)    t - tbl(1)

               g - grap(1)     r - refer(1)  v - vgrind(1)

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2

192.168.0.131  Bcast

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2-2

192.168.0.131  Bcast

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2-4

192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d :   -f 2

192.168.0.131  Bcast

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d ': ' -f 2   

cut: 分界符必須是單個字符

請嘗試執行"cut --help"來獲取更多信息。

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d '' -f 2   

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut '' -f 2   

cut: : 沒有那個文件或目錄

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2   

192.168.0.131  Bcast

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2|grep -v "Bcast"

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2|grep -v " Bcast"

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2

192.168.0.131  Bcast

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|cut -d : -f 2|cut -d ' ' -f 1

192.168.0.131

再試試用sed解出本題答案:

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|sed "s/inet addr:192.168.0.131/192.168.0.131/"

          192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

不行

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|sed "s/192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0/192.168.0.131/" 笨辦法也沒有達到想要的結果

          inet addr:192.168.0.131

[root@nginx_back ~]# ifconfig eth0|grep "inet addr"|sed "s/192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0/192.168.0.131/"|sed "s/inet addr:192.168.0.131/192.168.0.131/"

          192.168.0.131  重屬笨辦法

awk目前還沒有學,所以還不會,以後會補充上


老男孩老師的方法:

[root@nginx_back ~]# ifconfig eth0|grep "inet addr:"

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

[root@nginx_back ~]# ifconfig eth0|grep "inet addr:"|cut -d":" -f 2

192.168.0.131  Bcast

[root@nginx_back ~]# ifconfig eth0|grep "inet addr:"|cut -d":" -f 2|cut -d" " -f1

192.168.0.131

方法一、[root@nginx_back ~]# ifconfig eth0|grep "inet addr:"|cut -c 21-33

192.168.0.131

方法二、[root@nginx_back ~]# ifconfig eth0|sed -n "2p"

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

        [root@nginx_back ~]# ifconfig eth0|sed -n "2p"|cut -c 21-33

192.168.0.131

方法三、[root@nginx_back ~]# ifconfig eth0|awk 'NR==2'

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

        [root@nginx_back ~]# ifconfig eth0|awk 'NR==2'|cut -c 21-33

192.168.0.131

方法四、[root@nginx_back ~]# ifconfig eth0|head -2|tail -1|awk -F "[ :]+" '{print $4}'

192.168.0.131

推薦方法五、[root@nginx_back ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2 {print $4}'  “+“在這代表多個空格或多個冒號算一個分隔符           

192.168.0.131

解釋方法五、

[root@nginx_back ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:21:B6:B1  

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe21:b6b1/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000 

          RX bytes:9441211 (9.0 MiB)  TX bytes:11948352 (11.3 MiB)


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

192.168.0.131

“+“號在上面的方法五里面代表多個空格或多個冒號算一個分隔符,例如以下:

[root@nginx_back ~]# cat test.log(測試文件自己創建,內容如下)

-----------1@@@@@@@@@@2==========3

[root@nginx_back ~]# awk -F "[-@=]+" '{print $2,$3,$4}' test.log

1 2 3

方法六、[root@nginx_back ~]# ifconfig eth0|sed -n '2p'

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

[root@nginx_back ~]# ifconfig eth0|sed -n '2p'|sed 's/^.*addr://g'|sed 's/B.*$//g'

192.168.0.131

方法七、[root@nginx_back ~]# grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0

IPADDR=192.168.0.131

        [root@nginx_back ~]# grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0|awk -F "=" '{print $2}' 

192.168.0.131


sed練習:

[root@nginx_back ~]# ifconfig eth0|sed -n '2p'

          inet addr:192.168.0.131  Bcast:192.168.0.255  Mask:255.255.255.0

[root@nginx_back ~]# ifconfig eth0|sed -nr 's#^.*dr:(.*) Bcast:(.*) M.*$#\1\2#gp'

192.168.0.131 192.168.0.255 

[root@nginx_back ~]# ifconfig eth0|sed -n '4p'

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

[root@nginx_back ~]# ifconfig eth0|awk 'NR==4'

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

[root@nginx_back ~]# ifconfig eth0|sed -nr 's#^.*NN(.*) MULTI(.*) MT.*$#\1\2#gp' 

INGCAST 

[root@nginx_back ~]# ifconfig eth0|sed -nr 's#^.*NN(.*)MULTI(.*) MT.*$#\1\2#gp' 

ING CAST 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章