Chinaunix首页 | 论坛 | 博客
  • 博客访问: 263655
  • 博文数量: 94
  • 博客积分: 526
  • 博客等级: 中士
  • 技术积分: 687
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-09 10:02
文章存档

2014年(1)

2013年(10)

2012年(83)

分类:

2012-12-21 09:53:33

原文地址:sed常用实例学习 作者:qingheliu

自己学习的东西留为笔记

1:将文件中的某一行打印出来
[root@qht2 test]# cat a.txt 
123
456
789
[root@qht2 test]# sed -n '1p' a.txt 
123 
[root@qht2 test]# sed '/hello/p'  a.txt    ###含有hello的行显示,其他的也要显示
hello I am zhang
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345
678
1
end
[root@qht2 test]# sed -n '/hello/p'  a.txt ###仅显示包含hello字符串的行,即仅显示符合条件的行
hello I am zhang
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345
678
1
end

[root@qht2 test]# sed '/[0-9]/p' a.txt  
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345    ##含有数字的行显示2次
12345    ##含有数字的行显示2次
678
678
1
1
end
 
[root@qht2 test]# sed  -n '/[0-9]/p' a.txt   ##仅显示含有数字的行
12345
678
1
 


2:sed显示其中的几行
[root@qht2 test]# sed  -n '1,3p' a.txt 
123
456
789
[root@qht2 test]# sed -n '2,3p' a.txt 
456
789

3:删除其中的几行,但文件本身并没有修改  ##d学习
[root@qht2 test]# cat a.txt 
ab
bc
cd
de
ef
fg
hi
[root@qht2 test]# ls
a.txt
[root@qht2 test]# sed '2,3d' a.txt  ###将文件的第2和3行删除,但不影响整个文件的内容
ab
de
ef
fg
hi
[root@qht2 test]# cat b.txt 
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last
[root@qht2 test]# sed '$d' b.txt  ##删除文件的最后一行
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.


[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345
678
1
end
[root@qht2 test]# sed '/hello/d' a.txt  ###删除含有hello字符的行
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345
678
1
end

[root@qht2 test]# sed '/hello/!d' a.txt ###删除不包含hello字符的行,!d表示不删除
hello I am zhang
[root@qht2 test]# 

[root@qht2 test]# sed '/[0-9]/d' a.txt ##删除含有数字的行
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
end
[root@qht2 test]# sed '/[0-9]\{1\}/d' a.txt  ##删除含有1个数字的行
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
end
[root@qht2 test]# sed '/[0-9]\{3\}/d' a.txt  ##删除含有3个数字的行
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
1
end




[root@qht2 test]# sed 's/^...//g' a.txt    ##删除开头的三个字符
lo I am zhang
lo I am xiaojie
e to see you xiaojie
e to see you zhang too
45

1

[root@qht2 test]# sed 's/...$//g' a.txt  ##删除结尾的三个字符
hello I am zh
Hello I am xiao
nice to see you xiao
nice to see you zhang 
12

1




[root@qht2 test]# sed  '/^$/d'  a.txt  ##删除空行
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345
678
1
end
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345

678

1
end
[root@qht2 test]# sed  -i '/^$/d'  a.txt  ##-i表示执行后直接修改文件内容
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
12345
678
1
end

3:在某一行后添加内容
[root@qht2 test]# sed '2a hello xiaojie' a.txt  ##在第2行后添加hello xiaojie字符串
ab
bc
hello xiaojie
cd
de
ef
fg
hi

4:在某一行后添加几行
[root@qht2 test]# sed '2a hello\      ##在第2行后添加两行,行与行之间用\间隔
> xiaojie' a.txt
ab
bc
hello
xiaojie
cd
de
ef
fg
hi


5:字符串替换
[root@qht2 test]# sed '2,5c 555' a.txt   将2----5行替换成某个字符串或字符
ab
555
fg
hi
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I see
AAA1111
nice to see you xiaojie
nice to see you zhang 
12345
DDD
192.168.1.3
[root@qht2 test]# sed '1c\#!/bin/sh' a.txt     ##加上\可以替换
#!/bin/sh
Hello I am xiaojie
I see
AAA1111
nice to see you xiaojie
nice to see you zhang 
12345
DDD
192.168.1.3
[root@qht2 test]# sed '1c #!/bin/sh' a.txt     ####不加上\也可以替换
#!/bin/sh
Hello I am xiaojie
I see
AAA1111
nice to see you xiaojie
nice to see you zhang 
12345
DDD
192.168.1.3
[root@qht2 test]# 

文件本身并没有修改
6:sed替换字符串
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am liu
nice to see you liu
nice to see you zhang too
[root@qht2 test]# sed 's/liu/xiaojie/g' a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
[root@qht2 test]# sed -i 's/liu/xiaojie/g' a.txt 
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
[root@qht2 test]# sed '3,4s/zhang//g' a.txt  ###将3--4行中的zhang删掉(也可以理解为替换掉)
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you  t
12345
678
1
end
[root@qht2 test]# sed '/AAA/s/11/888/g' a.txt  ###将含有AAA行的11替换为888
hello I am zhang
Hello I am xiaojie
AAA888888
nice to see you xiaojie
nice to see you zhang too
12345
DDD
678
1
end




7:sed使用-i参数之间修改文件内容
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
[root@qht2 test]# ls
a.txt
[root@qht2 test]# sed -i '2,3d' a.txt 
[root@qht2 test]# ls
a.txt
[root@qht2 test]# cat a.txt 
hello I am zhang
nice to see you zhang too
[root@qht2 test]# 

8:
最后一行添加一行内容
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
[root@qht2 test]# sed -i '$a end' a.txt 
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
nice to see you xiaojie
nice to see you zhang too
end



9:显示前2行
[root@qht2 test]# sed '2q' b.txt 
Line one
The second line
[root@qht2 test]# head -n 2 b.txt ##head也可以实现
Line one
The second line
[root@qht2 test]# cat b.txt 
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last
[root@qht2 test]# 

10:-f表示sed应该从命令行上指定的文件中读取命令
[root@qht2 test]# cat print1_3 
1,3 p
[root@qht2 test]# sed -n -f print1_3  b.txt 
Line one
The second line
The third
[root@qht2 test]# cat b.txt 
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last



[root@qht2 test]# cat append_demo 
2 a\
alter.
[root@qht2 test]# sed -f append_demo  b.txt 
Line one
The second line
alter.
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last


[root@qht2 test]# cat insert_demo 
/This/ i\
BEFORE
[root@qht2 test]# sed -f insert_demo  b.txt 
Line one
The second line
The third
BEFORE
This is line four
Five 
BEFORE
This is the sixth sentence
BEFORE
This is line seven.
Eighth and last


[root@qht2 test]# cat subs_demo  ##将文件中的line替换成sentence,并显示出来 
s/line/sentence/p
[root@qht2 test]# cat b.txt 
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last
[root@qht2 test]# sed -n -f subs_demo  b.txt 
The second sentence
This is sentence four
This is sentence seven.
[root@qht2 test]# 


[root@qht2 test]# cat write_demo     ##将替换后的行写入到temp文件里
s/line/sentence/w temp
[root@qht2 test]# sed -f write_demo  b.txt  ##将b.txt中的line替换成sentence
Line one
The second sentence
The third
This is sentence four
Five 
This is the sixth sentence
This is sentence seven.
Eighth and last
[root@qht2 test]# ll
总计 32
-rw-r--r-- 1 root root  12 12-21 20:54 append_demo
-rw-r--r-- 1 root root 122 12-21 20:25 b.txt
-rw-r--r-- 1 root root  31 12-21 20:57 change_demo
-rw-r--r-- 1 root root  17 12-21 20:53 insert_demo
-rw-r--r-- 1 root root   6 12-21 20:46 print1_3
-rw-r--r-- 1 root root  18 12-21 21:12 subs_demo
-rw-r--r-- 1 root root  66 12-21 21:14 temp
-rw-r--r-- 1 root root  23 12-21 21:14 write_demo
[root@qht2 test]# cat temp 
The second sentence
This is sentence four
This is sentence seven.

[root@qht2 test]# cat write_demo2 
2,4 w temp2
[root@qht2 test]# sed -f write_demo2   b.txt 
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last
[root@qht2 test]# ll temp2 
-rw-r--r-- 1 root root 44 12-21 21:20 temp2
[root@qht2 test]# cat temp2 
The second line
The third
This is line four

[root@qht2 test]# sed -f write_demo3  b.txt 
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last
[root@qht2 test]# cat b.txt 
Line one
The second line
The third
This is line four
Five 
This is the sixth sentence
This is line seven.
Eighth and last
[root@qht2 test]# ll temp3 
-rw-r--r-- 1 root root 78 12-21 21:27 temp3
[root@qht2 test]# cat temp3 
Line one
Five 
This is the sixth sentence
This is line seven.
Eighth and last
 

11:将某行开头的nice替换成mynice
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I see
AAA1111
nice to see you xiaojie
nice to see you zhang too
12345
DDD
192.168.1.3
[root@qht2 test]# sed -n 's/^nice/mynice/p' a.txt    ##-n仅仅显示符合条件的记录
mynice to see you xiaojie
mynice to see you zhang too
[root@qht2 test]# 

[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I see
AAA1111
nice to see you xiaojie
nice to see you zhang too
12345
DDD
192.168.1.3


[root@qht2 test]# sed -i 's/see/&see/g' a.txt ##&表示替换换字符串中被找到的部份。
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I seesee
AAA1111
nice to seesee you xiaojie
nice to seesee you zhang too
12345
DDD
192.168.1.3

[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I seesee
AAA1111
nice to seesee you xiaojie
nice to seesee you zhang too
12345
DDD
192.168.1.3

[root@qht2 test]# sed -i 's#to#toto#g' a.txt  ###不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以“#”在这里是分隔符,代替了默认的“/”分隔符
[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I seesee
AAA1111
nice toto seesee you xiaojie
nice toto seesee you zhang totoo
12345
DDD
192.168.1.3




[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I see
AAA1111
nice to see you xiaojie
nice to see you zhang 
12345
DDD
192.168.1.3

[root@qht2 test]# sed 's/$/sedtsest/g' a.txt  ###每行的末尾用字符串sedtest替换。
hello I am zhangsedtsest
Hello I am xiaojiesedtsest
I seesedtsest
AAA1111sedtsest
nice to see you xiaojiesedtsest
nice to see you zhang sedtsest
12345sedtsest
DDDsedtsest
192.168.1.3sedtsest


[root@qht2 test]# cat a.txt 
hello I am zhang
Hello I am xiaojie
I see
AAA1111
nice to see you xiaojie
nice to see you zhang 
12345
DDD
192.168.1.3
[root@qht2 test]# sed -n  -e '1,4d' -e 's/nice/mynice/p' a.txt   ###-e指定多个命令
mynice to see you xiaojie
mynice to see you zhang 
[root@qht2 test]# sed   -e '1,4d' -e 's/nice/mynice/p' a.txt 
mynice to see you xiaojie
mynice to see you xiaojie
mynice to see you zhang 
mynice to see you zhang 
12345
DDD
192.168.1.3

12: -e  或-f 使用

13:
1~2 matches 1,3,5,7, etc.
2~2 matches 2,4,6,8, etc.
1~3 matches 1,4,7,10, etc.
2~3 matches 2,5,8,11, etc.



14:w 学习




阅读(482) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~