Skip to content

in-place editing of files

There are times, when mind is not exactly communicating with your hands – especially with their extremities called fingers….
There are times, when you wish you had some tool that could allow you to edit the mistakes of brain/fingers miscommunication on a number of hosts via some script….
AIX native version of sed is missing this ability but the “LINUX” version that you can install does it. So if your host has the /opt/freeware/bin/sed than the following will work for you:

sed -i -e 's/\bhello\b/jello/' hello.txt

Which replaces the first located hello with jello.

If you want to replace every hello with jello than do this instead:

 sed -i -e 's/\bhello\b/jello/g' hello.txt

But not all of us have this version of sed installed. What to do? You have perl. Using it to solve the previous leads to:

perl -pi -e 's/hello/jello/g' hello.txt

Do you know about any other way? If so, please let us now.

Today, I found a gem of simplicity in a shape of a comment. Marcin Wiśnos from showed me how to use the tool (vi) that is synonymous with UNIX to do what I described above. Apparently, there is no need for GNU sed or perl… 🙂

# vi +%s/ferris:/ferrisj:/g +wq user
"user" 13120 lines, 163733 characters "user" 13120 lines, 163734 characters
# grep ferrisj user

# grep -p ferrisj user
registry = LDAP
loginretries = 0

It works like a charm, thanks Marcin!

Posted in Real life AIX, scripts.

Tagged with , , , , .

5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Marcin Wiśnios says

    vi +%s/hello/jello/g +wq hello.txt

    It’s true pleasure to put in my 5 cents to your place, Mark.
    Best regards 😉

  2. Jeff says

    add an extension to the perl -i flag to automagically create a backup too

    perl -pi.old -e ‘s/new/old/g’ file

  3. MarkD:-) says


    this one I really have to try 🙂
    I looks like it is the best one so far…!



  4. MarkD:-) says


    thanks for your comment. Nothing beats being safe!


    MarkD 🙂

  5. MarkD:-) says


    You have a very impressive blog. Congrats!


Some HTML is OK

or, reply to this post via trackback.

WordPress Anti Spam by WP-SpamShield

Copyright © 2016 - 2018 Waldemar Mark Duszyk. All Rights Reserved. Created by Blog Copyright.