[root@freepp ~]# sed
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
(avoids change of input file ownership)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous
long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
--help display this help and exit
--version output version information and exit
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
E-mail bug reports to: bonzini@gnu.org .
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
[root@freepp ~]#
利用grep和sed提取IP地址:
[root@freepp ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:1A:92:37:C0:EB
inet addr:192.168.4.7 Bcast:192.168.4.255 Mask:255.255.255.0
inet6 addr: fe80::21a:92ff:fe37:c0eb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:595747 errors:0 dropped:0 overruns:0 frame:0
TX packets:548636 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:106966875 (102.0 MiB) TX bytes:470101083 (448.3 MiB)
Interrupt:201
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:72 errors:0 dropped:0 overruns:0 frame:0
TX packets:72 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3760 (3.6 KiB) TX bytes:3760 (3.6 KiB)
[root@freepp ~]#
ifconfig eth0 | grep 'inet addr' 是什么结果呢?
[root@freepp ~]# ifconfig eth0 | grep 'inet addr'
inet addr:192.168.4.7 Bcast:192.168.4.255 Mask:255.255.255.0
[root@freepp ~]#
啊哈,明显已经提取到所需的那一行
需要再处理
ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
[root@freepp ~]# ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.4.7 Bcast:192.168.4.255 Mask:255.255.255.0
[root@freepp ~]#
怎么样,看出变了吧,现在类似的要去掉IP后的内容即可
ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
[root@freepp ~]# ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.4.7
[root@freepp ~]#
是不是已经达到要求了。
ok,这里使用的是eth0 接口,那如果针对行呢??
使用n参数即可
[root@freepp ~]# ifconfig |sed -n '1,10'p| grep "inet addr"|sed s/^.*addr://g|sed s/.Bca.*$//g
192.168.4.7
[root@freepp ~]#
总结,利用grep提取ip地址的方法:ifconfig |sed -n '1,10'p| grep "inet addr"|sed s/^.*addr://g|sed s/.Bca.*$//g