首页 / LINUX / Linux三剑客之sed基本应用
Linux三剑客之sed基本应用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux三剑客之sed基本应用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2063字,纯文字阅读大概需要3分钟。
内容图文
sed:三剑客之第二实现对文件的增加 删除 修改 查询 过滤
命令格式:sed 选项 sed内置命令 文件
-i
-e
-n
a
i
d
p
s
g
创建文件tobedu.txt,输入内容,并以此文件为例。
[root@toby ~]# cat -n tobyedu.txt
1 I am toby teacher!
2 I like badminton ball ,billiard ball and chinese chess!
3 our site is tobyedu.com
4
5 my qq num is 12345678.
- 打印2到4行
[root@toby ~]# sed -n ‘2,4p‘ tobyedu.txt
I like badminton ball ,billiard ball and chinese chess!
our site is tobyedu.com
[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.
原文:http://blog.51cto.com/13499986/2115741
内容总结
以上是互联网集市为您收集整理的Linux三剑客之sed基本应用全部内容,希望文章能够帮你解决Linux三剑客之sed基本应用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。