Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

shell - How to insert a line using sed before a pattern and after a line number?

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/'

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can either write a sed script file and use:

sed -f sed.script file1 ...

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 ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...