Substitute a Keyword In a File

October 9th, 2006 liewsheng Posted in perl, sed, Text Manipulation | Hits: 44452 | 8 Comments »

Sometime I need to replace a keyword with another, I can’t just open a file and substitute 1 by 1. It is faster to using a single command line. There have more than 2 ways to do it, but I prefer this 2 command: perl and sed.

To substitute a word ‘abc’ to ‘def’ in a file xyz :

perl -pi -e 's/abc/def/;' xyz

or

sed -e 's/abc/def/' xyz > xyz_new

This 2 command will accept the RE(regular expression) as it input. for the perl command -p mean to loop inside the file, -i will replace in the file immediate match and -e specify the perl command to use.

Just like perl, -e provide to sed also represent the command to use. But the process text will print out to stdout not in the file, so we need to redirect it back to another file.

If you want to swap between 2 words, ‘abc def’ become ‘def abd’. Here want you need to enter:

perl -pi -e 's/(abc) (def)/$2$1/' xyz

or

sed -e 's/(abc) (def)/\2\1/' xyz > xyz_new

8 Responses to “Substitute a Keyword In a File”

  1. You might want to add the g flag at the end in order to substitute _all_ occurrences in a line:

    sed -e ‘s/abc/def/g’ xyz > xyz_new

  2. Great! g means global, search and replace it globally, instead of a line, it replace every single pattern it match. I usually like to do with -i. So it can be edit the file in place.

    sed -e ’s/abc/def/g’ -i xyz

  3. thanks for your post,great info.

  4. Thank you for your post. Loved it truly….

  5. I’m not that much of a online reader to be honest but your blogs really nice, keep
    it up! I’ll go ahead and bookmark your website to come back down the road.

    All the best

  6. hey there and thank you for your info – I’ve definitely picked
    up something new from right here. I did however expertise some technical issues using this
    web site, since I experienced to reload the website lots of times
    previous to I could get it to load properly. I had been wondering if your web host
    is OK? Not that I am complaining, but slow loading instances times
    will often affect your placement in google and can damage your
    quality score if ads and marketing with Adwords.
    Well I’m adding this RSS to my email and can look out for much more of your
    respective fascinating content. Ensure that you update this again soon.

  7. hello there and thank you for your information – I have certainly picked up something
    new from right here. I did however expertise a few technical
    issues using this site, since I experienced to reload the website lots of times previous to I could get
    it to load properly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but sluggish loading instances
    times will often affect your placement in google and can damage your high quality
    score if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and can look out for
    much more of your respective fascinating content. Ensure
    that you update this again soon.

  8. Definitely believe that which you said. Your favorite reason appeared to
    be on the web the simplest thing to be aware of.
    I say to you, I certainly get annoyed while people consider worries
    that they plainly don’t know about. You managed to hit the nail upon the top as
    well as defined out the whole thing without having side effect
    , people can take a signal. Will likely be back to get more.
    Thanks

Leave a Reply