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