Documente Academic
Documente Profesional
Documente Cultură
fahd.blog
Let the code do the talking...
Follow
$ cat file
line 1 View my complete profile
line 2
foo
line 3
line 4 Hi, I'm Fahd, a software developer at an
line 5 investment bank in London. I am passionate
bar about technology and work mainly with open
line 6 source software, specialising in Java
line 7 applications and Unix-based operating
systems.
1) Replace text on each line between two patterns (inclusive):
To perform a replacement on each line between foo and bar, including the lines containing foo and bar, use the This blog is a place for me to share useful
following: code snippets to solve problems that I have
come across, and to write about ideas and
$ sed '/foo/,/bar/{s/./x/g}' file experiences as a programmer.
line 1
line 2 All code on this blog has been written by me,
xxx unless stated otherwise, and you are free to
xxxxxx use, share and adapt it for any purpose,
xxxxxx under the terms of the GNU General Public
xxxxxx License.
xxx
I love hearing back from my readers, so
line 6
please feel free to leave comments! Thanks
line 7
for reading and happy programming :-)
2) Replace text on each line between two patterns (exclusive):
To perform a replacement on each line between foo and bar, excluding the lines containing foo and bar, use the
following: Follow @fahdshariff
http://fahdshariff.blogspot.sg/2012/12/sed-mutli-line-replacement-between-two.html 1/4
3/5/2018 fahd.blog: Sed: Mutli-Line Replacement Between Two Patterns
line 6 ► 2017 (17)
line 7 ► 2016 (20)
References:
Sed - An Introduction and Tutorial by Bruce Barnett
Posted by Fahd Shariff at 12:04 AM
Labels: commands, sed, UNIX
13 comments:
Reply 1 3 2 4 5 7 0
Reply
Nice article. In #5, how can I replace contents from another file? i.e. REPLACEMENT is contents of another file
Reply
jkellynet 7:24 PM
Thanks for taking the time to put this post together, it was very helpful!
Reply
lar0w 10:14 PM
However I must say that the number 6 needs a bit of tweaking. If you run the command with sed -n the output is fine.
But if you use sed -n -i, it double each line in the file.
http://fahdshariff.blogspot.sg/2012/12/sed-mutli-line-replacement-between-two.html 2/4
3/5/2018 fahd.blog: Sed: Mutli-Line Replacement Between Two Patterns
To get it work, I had to remove the first "p" after the bracket and also add my first pattern in the replacement (dunno
why the second one was not affected)
Larow
Reply
Reply
However just a missing piece that I couldn't work with above. How would you remove everything between foo and bar
without deleting full lines?
line 1
line 2 foo line 3
line 4
line 5 bar line 6
line 7
And I want to remove everything between foo and bar only where output will look like below.
line 1
line 2 line 6
line 7
Reply
Klaus 10:21 AM
Reply
Prueba 4:40 PM
Awesome, ty
Reply
Hello,
to this:
I am still trying to figure out how that works as there is not much documentation on it. It is probably something of the
sort "use the last regexp" or something.
Reply
fermario 8:07 PM
Reply
Reply
http://fahdshariff.blogspot.sg/2012/12/sed-mutli-line-replacement-between-two.html 3/4
3/5/2018 fahd.blog: Sed: Mutli-Line Replacement Between Two Patterns
Reply
http://fahdshariff.blogspot.sg/2012/12/sed-mutli-line-replacement-between-two.html 4/4