Find the answer to your Linux question:
Results 1 to 4 of 4
I need to substitute the value of a variable with the value of another variable I did a test # cat file this is needee # cp file file.old # ...
  1. #1
    Linux Newbie
    Join Date
    Feb 2007
    Posts
    248

    problem in substituting the values of variables via 'sed'

    I need to substitute the value of a variable with the value of another variable

    I did a test

    # cat file
    this is needee
    # cp file file.old

    # echo $STRING1
    this is needee

    # echo $STRING2
    this is NOT needee

    # cat file.old | sed 's/$STRING1/$STRING2/' > file

    # cat file
    this is needee

    i.e no change in the 'file'

    please help me in substituting the values of variables via 'sed'

  2. #2
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5
    # cat file.old | sed "s/$STRING1/$STRING2/" > file

    replace ' with " for variables

  3. #3
    Linux Newbie
    Join Date
    Feb 2007
    Posts
    248

    Exclamation

    Hi thanks stokilo, yes double quotes works

    another relevant question
    the following code has to do two task
    1, if there is no TIMEZONE set, then set it in /etc/sysconfig/clock
    2, if there is wrong TIMEZONE in /etc/sysconfig/clock, then fix it

    ok, now
    the proper value of timezone in /etc/sysconfig/clock should be
    TIMEZONE="Asia/Tashkent"

    right now the value of timezone(wrong) in /etc/sysconfig/clock is
    TIMEZONE="aa/tshknt"

    #!/bin/bash
    CLOCK=/etc/sysconfig/clock
    TIMEZONE_STRING='TIMEZONE="Asia/Tashkent"'
    CHECK_TIMEZONE=$(grep ^TIMEZONE $CLOCK)

    if [ -z "$CHECK_TIMEZONE" ]; then echo "$TIMEZONE_STRING" >> $CLOCK
    else cp "$CLOCK" /tmp/clock
    cat /tmp/clock | sed "s/$CHECK_TIMEZONE/$TIMEZONE_STRING/" > $CLOCK
    fi
    exit

    upon execution of the above code, I got the following error

    sed: -e expression #1, char 36: unknown option to `s'

  4. #4
    Just Joined!
    Join Date
    Mar 2009
    Posts
    5
    sed -e "s/$CHECK_TIMEZONE/$TIMEZONE_STRING/"

    Did you forget -e option ? I didn't check this script but try with -e switch.

    Best regards

    Slawek

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...