如何在模式之前和行号之后使用sed插入行? [英] How to insert a line using sed before a pattern and after a line number?
问题描述
如何在模式之前和行号之后使用sed
将行插入文件中?以及如何在Shell脚本中使用相同的内容?
How to insert a line into a file using sed
before a pattern and after a line number? And how to use the same in shell script?
这会在具有模式的每一行之前插入一行:
This inserts a line before every line with the pattern :
sed '/Sysadmin/i \ Linux Scripting' filename.txt
这会使用行号范围对此进行更改:
And this changes this using line number range :
sed '1,$ s/A/a/'
那么现在如何使用这两种方法(我不能)在模式之前,行号之后或其他方法之后使用sed
将行插入文件中?
So now how to use these both (which I couldn't) to insert a line into a file using sed
before a pattern and after a line number or another approach?
推荐答案
您可以编写sed脚本文件并使用:
You can either write a sed script file and use:
sed -f sed.script file1 ...
或者您可以使用(多个)-e 'command'
选项:
Or you can use (multiple) -e 'command'
options:
sed -e '/SysAdmin/i\
Linux Scripting' -e '1,$s/A/a/' file1 ...
如果您想在一行之后添加一些内容,则:
If you want to append something after a line, then:
sed -e '234a\
Text to insert after line 234' file1 ...
这篇关于如何在模式之前和行号之后使用sed插入行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!