标签:sed命令基本应用
sed:三剑客之第二[root@toby ~]#
2.查询不连续的行,只打印第1和第4行
[root@toby ~]# sed -n ‘1p;4p‘ tobyedu.txt
I am toby teacher!
[root@toby~]#
3.过滤出含有toby字符串的行.
[root@toby ~]# sed -n ‘/toby/p‘ tobyedu.txt
I am toby teacher!
our site is tobyedu.com
删除含有toby字符串的行
[root@toby ~]# sed ‘/toby/d‘ tobyedu.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 12345678.
4.将文件中的toby替换为tobygirl
[root@toby ~]# sed ‘s#toby#tobygirl#g‘ tobyedu.txt
I am tobygirl teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is tobygirledu.com
my qq num is 12345678.
5.将文件中的toby字符串全部替换为tobygirl且把qq号12345678替换为87654321
[root@toby ~]# sed -e ‘s#toby#tobygirl#g‘ tobyedu.txt -e ‘s#12345678#87654321#g‘
I am tobygirl teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is tobygirledu.com
my qq num is 87654321.
6.在tobyedu.txt文件第2行后追加文本i like linux。
[root@toby ~]# sed ‘2a i like linux‘ tobyedu.txt
I am toby teacher!
I like badminton ball ,billiard ball and chinese chess!
i like linux
our site is tobyedu.com
my qq num is 12345678.
7.在文件第2行插入文本i like tangwei
[root@toby ~]# sed ‘2i i like tangwei‘ tobyedu.txt
I am toby teacher!
i like tangwei
I like badminton ball ,billiard ball and chinese chess!
our site is tobyedu.com
my qq num is 12345678.
8.把第三行中的toby替换为xiaoting
[root@toby ~]# sed ‘3s#toby#xiaoting#g‘ tobyedu.txt
I am toby teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is xiaotingedu.com
my qq num is 12345678.
9.把第一行到第三行的toby字符串替换为xiaoting
[root@toby ~]# sed ‘1,3s#toby#xiaoting#g‘ tobyedu.txt
I am xiaoting teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is xiaotingedu.com
my qq num is 12345678.
标签:sed命令基本应用
原文地址:http://blog.51cto.com/13499986/2115741