Sunteți pe pagina 1din 4

3/5/2018 fahd.

blog: Sed: Mutli-Line Replacement Between Two Patterns

More Next Blog» sachin3392@gmail.com Dashboard Sign Out

fahd.blog
Let the code do the talking...

MONDAY, DECEMBER 31, 2012 SUBSCRIBE TO FAHD.BLOG

Sed: Mutli-Line Replacement Between Two Patterns


This post has some useful sed commands which can be used to perform replacements and deletes between two
patterns across multiple lines. For example, consider the following file: Fahd Shariff

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

$ sed '/foo/,/bar/{/foo/n;/bar/!{s/./x/g}}' file


line 1
line 2
foo POPULAR POSTS
xxxxxx
Analysing a Java Core Dump
xxxxxx
xxxxxx
Java 7: Working with Zip Files
bar
line 6 Changing Java Library Path at Runtime
line 7
Java 8: CompletableFuture vs Parallel
3) Delete lines between two patterns (inclusive): Stream
To delete all lines between foo and bar, including the lines containing foo and bar, use the same replacement sed
Retrying Operations in Java
command as shown above, but simply change the replacement expression to a delete.
Increase Console Output in Eclipse [Howto]
$ sed '/foo/,/bar/d' file
line 1
Writing your own Bash Completion Function
line 2
line 6 Useful Eclipse Templates for Faster Coding
line 7
Spring 3 - JavaConfig: Loading a Properties
4) Delete lines between two patterns (exclusive): File
To delete all lines between foo and bar, excluding the lines containing foo and bar, use the same replacement
sed command as shown above, but simply change the replacement expression to a delete. Sed: Mutli-Line Replacement Between Two
Patterns
$ sed '/foo/,/bar/ {/foo/n;/bar/!d}' file
line 1
line 2 BLOG ARCHIVE
foo ► 2018 (2)
bar

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)

5) Replace all lines between two patterns (inclusive): ► 2015 (13)


To perform a replacement on a block of lines between foo and bar, including the lines containing foo and bar, use: ► 2014 (19)
► 2013 (22)
$ sed -n '/foo/{:a;N;/bar/!ba;N;s/.*\n/REPLACEMENT\n/};p' file
line 1 ▼ 2012 (31)
line 2 ▼ December (3)
REPLACEMENT Sed: Mutli-Line Replacement Between
line 6 Two Patterns
line 7 Useless Use of Echo
Spring: Creating a java.util.Properties
How it works: Bean

/foo/{ # when "foo" is found ► November (1)


:a # create a label "a" ► October (2)
N # store the next line
► September (7)
/bar/!ba # goto "a" and keep looping and storing lines until "bar" is found
N # store the line containing "bar" ► August (5)
s/.*\n/REPLACEMENT\n/ # delete the lines ► June (1)
}
► May (1)
p # print
► April (3)
6) Replace all lines between two patterns (exclusive): ► March (3)
To perform a replacement on a block of lines between foo and bar, excluding the lines containing foo and bar,
► February (1)
use:
► January (4)
$ sed -n '/foo/{p;:a;N;/bar/!ba;s/.*\n/REPLACEMENT\n/};p' file
► 2011 (58)
line 1
line 2 ► 2010 (47)
foo ► 2009 (38)
REPLACEMENT ► 2008 (44)
bar
► 2007 (20)
line 6
line 7 ► 2006 (22)

References:
Sed - An Introduction and Tutorial by Bruce Barnett
Posted by Fahd Shariff at 12:04 AM
Labels: commands, sed, UNIX

13 comments:

sucheta 7:49 AM TOTAL PAGEVIEWS


Awesome post..
Really helpful...

Reply 1 3 2 4 5 7 0

jasmina dzomba 8:37 PM

very useful, thank you!!

Reply

Vishnu Agrawal 10:36 AM

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

Awesome article, quite helpful.

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)

Excuse for my bad english, I am french from Québec, Canada

Larow

Reply

Pradeep Kumar 9:53 PM

Very helpful! Thanks!

Reply

Kamran Shah 10:01 AM

Excellent work Fahd.

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?

So for example if I have a source of this text.

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

Many thanks in advance.

Reply

Klaus 10:21 AM

Really cool, thanks!

Reply

Prueba 4:40 PM

Awesome, ty

Reply

Atanas Hristozov 1:44 PM

Hello,

Apparently you can change this:

sed '/foo/,/bar/ {/foo/n;/bar/!d}' file

to this:

sed '/foo/,/bar/ {//!d}' file

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

Wow Sir very impressive your sed domain!

Reply

Suresh Kumar 9:45 AM

Great Article.Thanks a lot for help

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

mostafa kamal 2:06 PM

How to use a variable instead of "REPLACEMENT" string in number 6 example.


v="server 192.168.0.1"
sed -n '/foo/{p;:a;N;/bar/!ba;s/.*\n/$v\n/};p' file

Reply

Enter your comment...

Comment as: sachin taneja (G Sign out

Publish Preview Notify me

Links to this post


Create a Link

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

Picture Window theme. Powered by Blogger.

http://fahdshariff.blogspot.sg/2012/12/sed-mutli-line-replacement-between-two.html 4/4

S-ar putea să vă placă și