Home > Uncategorized > sed: replace a string only if it’s found in a given line

sed: replace a string only if it’s found in a given line

You can easily only perform a replacement against lines that contain a string with sed.

sed "regex to match s/replace this/with this/"

For example:

echo 'EVENT linkUp .1.3.6.1.6.3.1.1.5.4 "Status Events" Normal' | sed "/^EVENT.*Normal$/ s/Normal/wat/"

returns:

EVENT linkUp .1.3.6.1.6.3.1.1.5.4 "Status Events" wat

With reference:

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: