洞察探索如何通过一套代码实现跨平台小程序开发与高效管理,助力企业数字化转型
848
2022-08-27
sed初步
sed相关知识
sed是一个非交互式的文本编辑器,刻意地文本文件和标准输入进行编辑。sed可以一次性处理所有的编辑任务。 sed仅仅编辑源文件的副本,如果要保存编辑的结果则可以输出重定向至另一个文件。 三种使用sed的方法:
sed [option] 'sed command' inputfilesed [option] -f sed_script inputfile./sed_script inputfile
sed option
option | meaning |
-n | 不打印所有行到标准输出 |
-e | 将下一个字符串解析成sed编辑命令 |
-f | 正在调用sed脚本 |
sed使用定位文本行和sed编辑命令进行文本操作。 编辑编辑命令n的意义是读取下一个输入行。
sed定位
option | meaning |
x | x行 |
x,y | x到y行 |
/pattern/ | 查询包含模式的行 |
/pattern1/pattern2/ | 查询包含两个模式的行 |
/pattern/,x | 从与pattern匹配的行到x行之间的行 |
x,/pattern/ | 从x行到与pattern匹配的行之间的行 |
x,y | 查询不包括x和y行号的行 |
更加详细的内容直接man sed.
sed追加文本
sed '追加位置a\text' file a\表示换行追加文本。(后面插入) 正则表达式中的$表示行尾,但是在sed中却是最后一行的意思。 x,y!表示不在x,y行号范围内的行,值得注意的是!不能和/pattern/匹配。
sed插入文本
sed '插入位置i\text' file i\表示在匹配行的前面插入。 (前面插入)
sed修改文本
sed '修改位置c\text' file
sed删除文本
sed '删除位置d' file
sed替换文本
sed 's/被替换字符串/新字符串/[option]'
option | meaning |
g | 替换所有的被替换字符串 |
p | 与-n结合仅打印替换行 |
w filename | 重定向输出 |
注意:进行替换时如果没有g选项,那么仅仅替换所有匹配行的第一个匹配项。 如果option是数字,那么代表替换第几次出现的匹配项
sed变换命令
sed 'y/origin text/new text/' file 变换命令即是逐个字符替换 注意:origin text和new text必须等长
命令组
多条命令在同一位置执行可以包含在{}中,用分号隔开,和多条-e命令相比减少代码量。
二级命令提示符
在Bourne shell中可以使用二级命令提示符进行多行命令的输入。Bash shell是Bourne shell的扩展。
sed -n '> 2,3=> 2,3p> ' doc
sed小练习
练习文本doc: 《呼啸山庄》 14 Secret visits
1 1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 2 whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 3 ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying. ‘Cathy, 4 what's the matter,love?’I ashed. “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’ ‘I 5 hope he and I have years more of life ahead of us.All you need do is look after your father,and let him see you're cheerful.But I think 6 he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to 7 worry or annoy Father,'she promised.‘I only want him to recover.I love him more than anyone else in the world,more than myself!’ 8 Just then we reached the gate,and I saw a gentleman on a horse looking over it.It was Heathcliff. ‘Miss Linton!’he called.There's 9 something I must tell
打印doc文本的2至4行:
$ sed -n '2,4p' docwhole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying. ‘Cathy,what's the matter,love?’I ashed. “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’ ‘I
打印doc中含有关键字Cathy的行
$ sed -n '/Cathy/p' doc whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had ended,and was very worried about her father's illness.One day when we were walking in the
打印doc中含有关键字Cathy的行及行号
$ sed -n -e '/Cathy/=' -e '/Cathy/p' doc # 等价于$ sed '/Cathy/{=;p}' doc2whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had 3ended,and was very worried about her father's illness.One day when we were walking in the
在第一行中追加内容”this is append context.”
$ sed -n -e '1a\this is append context' -e '1,2p' doc1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the this is append contextwhole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with
在匹配the Grange garden的行下追加两行文本
hello world I love linux. sed脚本:
#!/bin/sed -f
执行:
./append.sed doc1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying. ‘Cathy, hello world I love linux.what's the matter,love?’I ashed. “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’ ‘I hope he and I have years more of life ahead of us.All you need do is look after your father,and
打印匹配the Grange garden的行到最后一行
$ sed -n -e '/the Grange garden/,$=' -e '/the Grange garden/,$p' doc3ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying. ‘Cathy,4what's the matter,love?’I ashed. “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’ ‘I 5hope he and I have years more of life ahead of us.All you need do is look after your father,and let him see you're cheerful.But I think6 he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to 7 worry or annoy Father,'she promised.‘I only want him to recover.I love him more than anyone else in the world,more than myself!’8Just then we reached the gate,and I saw a gentleman on a horse looking over it.It was Heathcliff. ‘Miss Linton!’he called.There's 9something I must tell
在匹配the Grange garden的行上方插入文本
I love linux.
sed -n -e '/the Grange garden/i\I love linux' -e '/the Grange garden/p' docI love linuxended,and was very worried about her father's illness.One day when we were walking in the
注意: 插入内容默认是打印的。
将匹配the Grange garden的行替换成I love linux
$ sed -n -e '1,4=' -e '/the Grange garden/c\I love linux' -e '1,4p' doc11800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the 2whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had3I love linux4what's the matter,love?’I ashed. “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’ ‘I
删除4至最后一行
$ cat delete.sed sed -e '4,$d' doc$ chmod +x delete.sed $ ./delete.sed 1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the whole winter,so Cathy only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had ended,and was very worried about her father's illness.One day when we were walking in the
删除含有Linton的行,不区分大小写并保存结果
sed '/[Ll][Ii][Nn][Tt][Oo][Nn]/d' doc > read
替换所有Cathy成凯西,并w重定向到read文本
sed -n -e 's/Cathy/凯西/g' -e 'w read' doc cat read
1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the whole winter,so 凯西 only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying. ‘凯西,what's the matter,love?’I ashed. “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’ ‘I hope he and I have years more of life ahead of us.All you need do is look after your father,and let him see you're cheerful.But I think he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to worry or annoy Father,'she promised.‘I only want him to recover.I love him more than anyone else in the world,more than myself!’Just then we reached the gate,and I saw a gentleman on a horse looking over it.It was Heathcliff. ‘Miss Linton!’he called.There's something I must tell
所有Cathy加上“()”
$ sed -n 's/Cathy/(&)/pg' docwhole winter,so (Cathy) only had me as a companion on her walks.She had become very quiet and sad since her relationship with Linton had ended,and was very worried about her father's illness.One day when we were walking in the Grange garden,I noticed her crying. ‘(Cathy),$ sed -n 's/Cathy/(cathy)/pg' doc
将doc的5至7行写入到read文本中
sed -n '5,7 w read' doc
将doc1和doc2合并成一个新的文件,原文件不受影响
doc1:
this is
doc2:
this is
sed '$r doc2' doc1 > doc3
寻找doc中带有’E’开头的词的所有行
sed -n '/E.*/p' doc1800 The months passed,and soon it was autumn.Mr Edgar caught a bad cold,which seemed to get worse and worse.He stayed indoors the what's the matter,love?’I ashed. “Oh,Ellen,'she sobbed,‘what shall I do if Father dies? And if you die?I'll be left alone!’ ‘I he would be really ill if he thought you loved Linton,whose father would like to see Mr Edgar dead.’I'll never,never do anything to
将doc中大写的“MECA”全都替换成相应的小写字符
sed 'y/MECA/meca/' doc
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~