Linux三剑客之sed基本应用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux三剑客之sed基本应用相关的知识,希望对你有一定的参考价值。
sed:三剑客之第二
实现对文件的增加 删除 修改 查询 过滤
命令格式:sed 选项 sed内置命令 文件
-i
-e
-n
a
i
d
p
s
g
创建文件tobedu.txt,输入内容,并以此文件为例。
[[email protected] ~]# 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行
[[email protected] ~]# sed -n ‘2,4p‘ tobyedu.txt
I like badminton ball ,billiard ball and chinese chess!
our site is tobyedu.com
[[email protected] ~]#
2.查询不连续的行,只打印第1和第4行
[[email protected] ~]# sed -n ‘1p;4p‘ tobyedu.txt
I am toby teacher!
[[email protected]~]#
3.过滤出含有toby字符串的行.
[[email protected] ~]# sed -n ‘/toby/p‘ tobyedu.txt
I am toby teacher!
our site is tobyedu.com
删除含有toby字符串的行
[[email protected] ~]# sed ‘/toby/d‘ tobyedu.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 12345678.
4.将文件中的toby替换为tobygirl
[[email protected] ~]# 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
[[email protected] ~]# 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。
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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.
以上是关于Linux三剑客之sed基本应用的主要内容,如果未能解决你的问题,请参考以下文章