#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Global Replacement of words


    I have a paragraph in a flat file and need to replace 2 words within it. Is it possible to do this with 1 command?

    Currently I do it this way:

    This file is owned by paul
    This file is owned by paul
    This file is owned by paul

    %s/paul/alex/g

    What i'd like to do is create a command that would replace say both file and paul with alex directory

    Kind Regards
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    Have a look at sed ...

    Code:
    sed -e 's/paul/alex/g' -e 's/file/directory/g' YourInput.txt > NewFile.txt
    Warning - from memory and not tested!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Phoenix AZ
    Posts
    30
    Rep Power
    19

    Correct


    I can't use the -e, tho, on my system, so I code this way:

    Code:
    sed "s*oldvalue%newvalue%g" oldfilename > newfilename
    
    rm oldfilename
    mv newfilename oldfilename
    In a ksh script, I run this for multiple files when I know that the value I need to replace exists in more than one file:

    Code:
    for f in *; do
        if [ -f $f ]; then
            sed "s%$oldval%$newval%g" $f > $f.tmp
            rm $f
            mv $f.tmp $f
        fi
    done
    Programmer at large

IMN logo majestic logo threadwatch logo seochat tools logo