UNIX script URGENT!!!

#!/bin/sh

INPUT () {
    echo "Please enter line number: "
    read LINE

    echo "Please enter column number that you want to modify: "
    read COLUMN

    head -$LINE dataf > line1;
    LINE=`expr $LINE + 1`
    head -$LINE dataf > line2;

    diff line1 line2 > tmp;
    sed -e 's/;/ /g' tmp > tmp2;
    COLUMN=`expr $COLUMN + 1`
    OLDV=`awk '{printf "%s\n",$'$COLUMN' ; }' tmp2`
    echo "The value is $OLDV \n"

    echo "Please enter new value of the column: "
    read NEWV
    sed -e 's/$OLDV/$NEWV/g' tmp2 > tmp3

}

OthersMod () {
    echo "Do you want to do other modifications [y/n]?"
    read ANS

while [ "$ANS" = y ] 
do

    INPUT
    OthersMod 

done
}


INPUT
OthersMod

This is the whole script. The problem is:

    echo "Please enter new value of the column: "
    read NEWV
    sed -e 's/$OLDV/$NEWV/g' tmp2 > tmp3

I can't change the old value to the new value. Any wrong with the script?? Please help.. THANKS!

Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.

Thank you.

The UNIX and Linux Forums

1 Like

try having double quotes around the below sed command instead of single..

sed -e "s/$OLDV/$NEWV/g" tmp2 > tmp3
1 Like

I tried but it doesn't work. It gave me this:
sed: command garbled: s/

Try :

sed 's/'"$OLDV"'/'"$NEW"'/g' tmp2 > tmp3

for me it works :

# a=t
# b=Z
# echo "toto tutu toto tatat test" | sed 's/'"$a"'/'"$b"'/g'
ZoZo ZuZu ZoZo ZaZaZ ZesZ

if it doesn't work, then try

eval sed -e 's/$OLDV/$NEW/g' tmp2 > tmp3

(the -e might be useless)

if nothing else works, you can give a try to this "useless backtick/echo award"

sed 's/'`echo "$OLDV"`'/'`echo "$NEW"`'/g' tmp2 > tmp3

It still doesn't work. Anyway, thanks for ur effort! :slight_smile:

or just

eval "sed -e 's/$OLDV/$NEWV/g'" tmp2 > tmp3

Watch out for the double quote and simple quote !

# a=t
# b=Z
# echo "toto tutu toto tatat test" | eval "sed 's/$a/$b/g'"
ZoZo ZuZu ZoZo ZaZaZ ZesZ
#

Thanks but still can't..

Show me a copy/paste of what you entered and the error msg you got

#!/bin/sh

INPUT () {
    echo "Please enter line number: "
    read LINE

    echo "Please enter column number that you want to modify: "
    read COLUMN

    head -$LINE dataf > line1;
    LINE=`expr $LINE + 1`
    head -$LINE dataf > line2;

    diff line1 line2 > tmp;
    sed -e 's/;/ /g' tmp > tmp2;
    COLUMN=`expr $COLUMN + 1`
    OLDV=`awk '{printf "%s\n",$'$COLUMN' ; }' tmp2`
    echo "The value is $OLDV \n"

    echo "Please enter new value of the column: "
    read NEWV
    sed 's/'`echo "$OLDV"`'/'`echo "$NEWV"`'/g' tmp2 > tmp3
    cat tmp3

}

OthersMod () {
    echo "Do you want to do other modifications [y/n]?"
    read ANS

while [ "$ANS" = y ] 
do

    INPUT
    OthersMod 

done
}


INPUT
OthersMod

Error msg: sed: command garbled: s/

Result: the data still didn't change.

hmm!.. the double quoted sed command must work. Anyway try the following and there is no need for -e option, in this case.

sed  "s/${OLDV}/${NEWV}/g" tmp2 > tmp3

try with (for me it works), if it doesn't work, give a try changing simple quote into double.

 eval sed 's/$OLDV/$NEWV/g' tmp2 > tmp3

haha! I also don't know why the double quote can't work. Anyway, still can't work.
same error msg.

---------- Post updated at 05:25 PM ---------- Previous update was at 05:24 PM ----------

still can't work. And with the same error msg.. hmm... still wondering what's the problem.... :frowning:

change
#!/bin/sh with #!/bin/ksh (ksh is sh compliant)

Hmmm... maybe there are some sh options that can desactivate some stuff in interpretation...

are you sure the error message is for this double quoted sed and not the for the previous sed you have in the function..? Make sure the first sed command (sed -e 's/;/ /g' tmp > tmp2;) works fine and yields the expected output.

yea.. the first sed command works pretty fine..

There is an unwanted newline in the variable OLDV which is upsetting the "sed".

Try this:

    OLDV=`awk '{printf "%s",$'$COLUMN' ; }' tmp2`

The whole process is a bit weird (especially the use of "diff").
Can we see:
Sample data
Sample answers and matching expected output.

Ok then give a try to :

sed 's/'`echo $OLDV`'/'`echo $NEW`'/g' tmp2 > tmp3

with no quote around the $var so the echo should trail the new line causing the error

The script in post #10 just needed the awk corrected to repair the error seen from the corrected "sed" (the one with double quotes).
The whole script has dubious logic and we need to know what it is meant to do.