One Day One Cmd

Place for playing with commands…

Day 24 : $ sed

ODOC: $sed.Use the s/pattern/replacement/ command to substitute text matching the ‘pattern’ with the ‘replacement’. ….

eg: $ sed -e ’s/thru/through/g’ in-file >out-file.It will replace ‘thru’ with ‘through’ in in-file &stored in out-file./g for replacing in all occurances.:-)


2 responses to “Day 24 : $ sed

  1. Pankaj August 12, 2010 at 7:36 am

    In sed command instead of / we can also use , # ! | as a separator.

    let say…. we want to replace /usr/bin/ with /usr/local/bin/ from the file, we can do this like

    sed “s,/usr/bin/,/usr/local/bin/,g” input-file > output-file
    sed “s!/usr/bin/!/usr/local/bin/!g” input-file > output-file

    we can use any of the , # ! | separator in accordance with what string we want to replace.

    If we want to make the changes in the same input file instead of redirecting to any output file, then we can use -i option.
    for example,
    sed -i “s,/usr/bin/,/usr/local/bin/,g” input-file
    Changes will reflect in the same input-file

Leave a Reply

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

You are commenting using your 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 )


Connecting to %s

%d bloggers like this: