String replacement shell script on AIX

5,798

Shell variables only resolve between Double Quotes (").

sed "s/$WORKDIR/$ARCHIVE/ig" test.dat > abc (Double quotes)

Would work if not for the forward slashes. Sed can use any character to delimit those input fields and forward slash is perhaps not the best choice due to it's use for directory paths. For example you can use this instead:

sed "s#$WORKDIR#$ARCHIVE#ig" test.dat > abc

Share:
5,798

Related videos on Youtube

Grv
Author by

Grv

Just started coding.

Updated on September 18, 2022

Comments

  • Grv
    Grv over 1 year

    I am using AIX, and there is not an -i option available in the version of sed I am using:

    sed: illegal option -- i
    Usage:  sed [-n] Script [File ...]
            sed [-n] [-e Script] ... [-f Script_file] ... [File ...]
    

    I want to replace the path of the directory in one of the file using a script; I am trying like this:

    WORKDIR="/workdir/liv/spool"
    ARCHIVE="u/user/new"
    
    sed 's/$WORKDIR/$ARCHIVE/ig' test.dat > abc
    mv abc test.dat
    

    which gives the error:

    sed: Function s/$WORKDIR/$STRATIXARCHIVE/ig cannot be parsed.
    

    I would like to replace all occurrences same as $WORKDIR with $$ARCHIVE

  • glenn jackman
    glenn jackman almost 9 years
    It won't, because there are slashes in the variable contents: use different delimiters for the s command: sed "s:$WORKDIR:$ARCHIVE:ig" test.dat > abc