1. 学习现状
很多人在学习sed命令时会去看网上的视频,网上教的东西只有皮毛,很多sed的用法介绍的并不全面,导致很多人在学习过程中容易忘记sed的用法;本人观点,国内的很多资料都很片面;我从国外的网站上把比较全面的内容分享给大家,并加入自己的理解,满满的干货保证一学就会;希望大家能够从足够的案例中学会最常见的用法;
2. sed的用法
语法
sed OPTIONS... [SCRIPT] [INPUTFILE...]
s 表示substitute,替换的意思,s/开头就是执行替换操作
d 表示delete,替换的意思,/d结尾就是执行删除操作
p表示print,替换的意思,/p结尾就是执行内部打印操作
g表示global,全局的意思,/g结尾就是执行全局操作
-n 是关闭内部打印,就是匹配的行不回重复打印;
^表示开头
$表示结尾
G表示空行
参考文本1
[root@kdc-server ~]# cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
^C
[root@kdc-server ~]# cat geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
替换操作
用例1 每一行的第一个出现“unix”的位置都会被替换成“linux”
[root@kdc-server ~]# sed 's/unix/linux/' geekfile.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
用例2 每一行的第3个出现“unix”的位置都会被替换成“linx”
root@kdc-server ~]# sed 's/unix/linx/3' geekfile.txt
unix is great os. unix is opensource. linx is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linx .unix is a powerful.
用例3 每一行出现“unix”的位置都会被替换成“linx”
[root@kdc-server ~]# sed 's/unix/linx/g' geekfile.txt
linx is great os. linx is opensource. linx is free os.
learn operating system.
linx linux which one you choose.
linx is easy to learn.linx is a multiuser os.Learn linx .linx is a powerful.
用例4 将1到3行中第一个出现“unix”的位置都会被替换成“linux”
[root@kdc-server ~]# sed '1,3 s/unix/linux/' geekfile.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
用例5 将所有行中出现“unix”的位置都会被替换成“linux”
[root@kdc-server ~]# sed 's/unix/linx/g' geekfile.txt
linx is great os. linx is opensource. linx is free os.
learn operating system.
linx linux which one you choose.
linx is easy to learn.linx is a multiuser os.Learn linx .linx is a powerful.
用例6 将第1行到第3行中出现“unix”的位置都会被替换成“linux”
[root@kdc-server ~]# sed '1,3 s/unix/linux/g' geekfile.txt
linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
用例7 将第3行中第一个位置出现“unix”的位置都会被替换成“linux”
sed '3 s/unix/linux/' geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
用例8 将所有行中出现“unix”的位置都会被替换成“linux”,并二次打印存在替换的行,不变更的只打印一次;
[root@kdc-server ~]# sed 's/unix/linux/p' geekfile.txt
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
用例9 将所有行中出现“unix”的位置都会被替换成“linux”,并只打印存在替换的行;
[root@kdc-server ~]# sed -n 's/unix/linux/p' geekfile.txt
linux is great os. unix is opensource. unix is free os.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
用例10 将第3行中所有出现“unix”的位置都会被替换成“linux”
[root@kdc-server ~]# sed 's/unix/linux/3g' geekfile.txt
unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.
用例11 将第2行到最后一行中第一个出现“unix”的位置都会被替换成“linux”
[root@kdc-server ~]# sed '2,$ s/unix/linux/' geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful
删除操作
用例1 删除第4行
[root@kdc-server ~]# sed '4d' geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
用例2 删除最后一行
[root@kdc-server ~]# sed '$d' geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
用例3 删除第2行到最后一行
[root@kdc-server ~]# sed '2,$d' geekfile.txt
unix is great os. unix is opensource. unix is free os.
用例4 删除第2行到第4行
[root@kdc-server ~]# sed '2,4d' geekfile.txt
unix is great os. unix is opensource. unix is free os.
用例5 删除匹配到unix的行
[root@kdc-server ~]# sed '/unix/d' geekfile.txt
learn operating system.
用例6 匹配带easy的行,并往下加两行进行删除
[root@kdc-server ~]# sed '/easy/,+2d' a.txt
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
其他用法
参考文本2
[root@kdc-server ~]# cat > a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
用例1 给每行后面插入一个空行
[root@kdc-server ~]# sed G a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
用例2 给每行后面插入2个空行
[root@kdc-server ~]# sed 'G;G' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
用例3 删除所有的空行
[root@kdc-server ~]# sed '/^$/d' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
用例4 匹配带有love的行,并在前后添加空行
[root@kdc-server ~]# sed '/love/{x;p;x}' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
查询功能
用例1 查询第2行到第5行的内容
[root@kdc-server ~]# sed -n '2,5p' a.txt
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
用例2 查看除第二到第四行的内容
[root@kdc-server ~]# sed '2,4d' a.txt
life isn't meant to be easy, life is meant to be lived.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
用例3 查看第四行的内容
[root@kdc-server ~]# sed -n '4'p a.txt
Don’t hesitate to ask for love & don’t hesitate to show love too.
用例4 查看第4到第6行的内容
[root@kdc-server ~]# sed -n '4,6'p a.txt
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
用例5 查看第3行到最后一行的内容
[root@kdc-server ~]# sed -n '3,$'p a.txt
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
用例6 查看匹配到every的行
[root@kdc-server ~]# sed -n /every/p a.txt
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
用例7 查看到匹配到everyone的行到第5行
[root@kdc-server ~]# sed -n '/everyone/,5p' a.txt
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
用例8 查看从第1行开始,直到匹配到everyone的行
[root@kdc-server ~]# sed -n '1,/everyone/p' a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
用例9 查看查看匹配到learn的行再多两行
[root@kdc-server ~]# sed -n '/learn/,+2p' a.txt
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.