nl的作用:
nl - number lines of files打印出文件的内容,并每行前加上行号。
有一点需要注意的就是nl在空行前是不显示行号的~
小节一:以行为单位的新增/删除功能
1. nl /etc/passwd | sed '2,5d'【将/etc/passwd的内容列出并且列印行号,同时,请将第 2~5 行删除】
[root@sor-sys ~]# nl /etc/passwd | sed '2,5d'
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 news:x:9:13:news:/etc/news:
2. nl /etc/passwd | sed '2a drink tea'【在第二行后(亦即是加在第三行)加上『drink tea?』字样!】
[root@sor-sys ~]# nl /etc/passwd | sed '2a drink tea'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
drink tea
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 news:x:9:13:news:/etc/news:
3. nl /etc/passwd | sed '2a Drink tea or \
drink beer ?'【在第二行后面加入两行字,例如Drink tea or .....与drink beer?】
[root@sor-sys ~]# nl /etc/passwd | sed '2a Drink tea or \
> drink beer ?'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or
drink beer ?
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 news:x:9:13:news:/etc/news:
小节二:以行为单位的取代与显示功能
1. nl /etc/passwd | sed '2,5c No 2-5 number'【将第2-5行的内容取代成为No 2-5 number】
[root@sor-sys ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
1 root:x:0:0:root:/root:/bin/bash
No 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 news:x:9:13:news:/etc/news:
2. nl /etc/passwd | sed -n '5,7p'【仅列出 /etc/passwd 文件内的第 5-7 行】
[root@sor-sys ~]# nl /etc/passwd | sed -n '5,7p'
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
小节三:部分数据的搜寻并取代的功能
1. /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'【获取主机ip】
[root@sor-sys ~]# /sbin/ifconfig eth0 | grep 'inet addr'
inet addr:192.168.55.229 Bcast:192.168.55.255 Mask:255.255.255.0
[root@sor-sys ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.55.229 Bcast:192.168.55.255 Mask:255.255.255.0
[root@sor-sys ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.55.229
[root@sor-sys ~]#
假如说我们的ip比较多的话,正巧你想要获得eth0,那么我们可以用个比较高级的用法:
[root@sor-sys ~]# ifconfig eth0 | awk -F'[ :]+''NR==2{print $4}'
192.168.55.229
为了方便你观察各部分的内容,我就多打印了一些信息:
[root@sor-sys ~]# ifconfig eth0 | awk -F'[ :]+' 'NR==2{print $1}'
[root@sor-sys ~]# ifconfig eth0 | awk -F'[ :]+' 'NR==2{print $2}'
inet
[root@sor-sys ~]# ifconfig eth0 | awk -F'[ :]+' 'NR==2{print $3}'
addr
[root@sor-sys ~]# ifconfig eth0 | awk -F'[ :]+' 'NR==2{print $5}'
Bcast
2. 假设我只要 MAN 存在的那几行数据, 但是含有 # 在内的注解我不想要,而且空白行我也不要!此时该如何处理呢?
注意:以下结果我只是截取了一部分
[root@sor-sys ~]# cat /etc/man.config | grep 'MAN'
# when MANPATH contains an empty substring), to find out where the cat
# MANBIN pathname
# MANPATH manpath_element [corresponding_catdir]
# MANPATH_MAP path_element manpath_element
# MANBIN /usr/local/bin/man
# Every automatically generated MANPATH includes these fields
MANPATH /usr/man
MANPATH /usr/share/man
MANPATH /usr/local/man
MANPATH /usr/local/share/man
MANPATH /usr/X11R6/man
[root@sor-sys ~]# cat /etc/man.config | grep 'MAN'| sed 's/#.*$//g'
MANPATH /usr/man
MANPATH /usr/share/man
MANPATH /usr/local/man
MANPATH /usr/local/share/man
MANPATH /usr/X11R6/man
[root@sor-sys ~]# cat /etc/man.config | grep 'MAN'| sed 's/#.*$//g' | sed '/^$/d'
MANPATH /usr/man
MANPATH /usr/share/man
MANPATH /usr/local/man
MANPATH /usr/local/share/man
MANPATH /usr/X11R6/man
小节四:直接修改文件内容(危险动作)
1. sed -i 's/\.$/\!/g' regular_express.txt【利用sed将regular_express.txt内每一行结尾若为.则换成!】
上面的分隔符可以替换成#,下面看看是如何写的:
sed -i 's#\!$#\.#g' regular_express.txt【注意:那个。和!前面的转义符最好加上,如若不加,你会发现将所有的其他结尾的符号也都改变了~】
#上头的-i选项可以让你的sed直接去修改后面接的文件内容而不是由屏幕输出!
#这个范例是用在取代!请您自行 cat 该文件去查阅结果罗!
[root@sor-sys zy]# cat regular_express.txt
This is zhuying.
[root@sor-sys zy]# sed -i 's/\.$/\!/g' regular_express.txt
[root@sor-sys zy]# cat regular_express.txt
This is zhuying!
2. sed -i '$a # This is a test' regular_express.txt【利用sed直接在regular_express.txt最后一行加入# This is a test】
由于$代表的是最后一行,而a的动作是新增,因此就在该文件最后新增内容!
[root@sor-sys zy]# sed -i '$a # This is a test' regular_express.txt
[root@sor-sys zy]# cat regular_express.txt
This is zhuying!
# This is a test
3. sed -i '$a \\' regular_express.txt 【在最后一行新增一空行】
[root@sor-sys zy]# sed '$a \\' regular_express.txt
This is zhuying!
# This is a test
#This is a joke,are you kidding?
[root@sor-sys zy]#
4.直接删除某行信息
[oracle@sys-1 weblogic]$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.)
BUILD-JDK: 1.5
Target-JDK: 1.5
Build-By: Administrator
Build-On: 06/11/2012 12:23 PM
Build-Version: 4.3.44
Build-Number: 2
Weblogic-Application-Version: v1
[oracle@sys-1 weblogic]$
[oracle@sys-1 weblogic]$ sed -i'/Weblogic-Application-Version/d' META-INF/MANIFEST.MF
[oracle@sys-1 weblogic]$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.)
BUILD-JDK: 1.5
Target-JDK: 1.5
Build-By: Administrator
Build-On: 06/11/2012 12:23 PM
Build-Version: 4.3.44
Build-Number: 2
[oracle@sys-1 weblogic]$
切忌:不可使用\n,这个在双引号中生效,但貌似在sed中不行哦,下面我们来看一下!
[root@sor-sys zy]# sed '$a \n' regular_express.txt
This is zhuying!
# This is a test
#This is a joke,are you kidding?
n
[root@sor-sys zy]# sed '$a "\n"' regular_express.txt
This is zhuying!
# This is a test
#This is a joke,are you kidding?
"
"
[root@sor-sys zy]#
延伸:
[oracle@sor-sys zy]$ cat test
111
2
333
4
5
6
下面是删除第一行到匹配行:
[oracle@sor-sys zy]$ sed '1,/333/d' test
4
5
6
下面是匹配行到末行:
[oracle@sor-sys zy]$ sed -n '/333/,$p' test
333
4
5
6
[oracle@sor-sys zy]$
tac是按照首字母进行反排序,然后再进行一次反排序就正常了:
tac是cat的倒写,意思也是和cat相反的;cat是从第一行显示到最后一行,而tac是从最后一行显示到第一行。
[oracle@sor-sys zy]$ tac test | sed '/3/q' | tac
333
4
5
6
[oracle@sor-sys zy]$