Search and replace multi-line expressions with SED

Now here’s an interesting problem:
I wanted to do a recursive search and replace in unix, AND I wanted to do an expression that spans multiple lines. Here’s what I came up with:

find ./ -type f | xargs sed -E -i -n
'1h;1!H;${;g;s/<\/fileSet>.*<fileSet>.*RevisionVersion.*
<\/fileSet>.*<\/fileSets>/<\/fileSet>\n<\/fileSets>/g;p}'

There a lot of examples showing you how to do this.
The first argument lists all files recursively. These are the piped to sed, which uses an inline search and replace (-i or –in-line), then using the expression ‘{}’ which is then modified for multi-line expressions (1h;1!H;).

Advertisements

One comment

  1. Joon · January 3, 2012

    I enjoy your blog a lot!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s