Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED Quotes in or out of script

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Hi All,

I'm having the usual nightmare trying to get a sed command to accept variables in a substitution; It works on the command line and I can see that the shell has interpolated the variable correctly by using echo, but when I run it in the script it just doesn't update.

File: Pre SED

IT412 587XX 7
IT412 627P5 7
IT412 133D4 39.5

From command line;
sed 's/IT412 587XX 7/IT412 587XX '"$V587XX"'/' testfile > testfile1

cat testfile1;

IT412 587XX 721.00
IT412 627P5 7
IT412 133D4 39.5

echo from script;

sed 's/IT412 587XX 7/IT412 587XX '721.84'/'

Run via script;

Totalswifts=`awk '(NR>1){sum +=$3}END{print sum - 53.5}' $SWIFTFTP/$FILENAME.txt`
V587XX=`expr $Totalswifts*0.07 | bc`
V627P5=`expr $Totalswifts*0.07 | bc`
V133D4=`expr $Totalswifts*0.395 | bc`

sed 's/IT412 587XX 7/IT412 587XX '"$V587XX"'/' $SWIFTFTP/$FILENAME.txt > /tmp/testfile1

OUTPUT;
cat testfile1
IT412 587XX 7
IT412 627P5 7
IT412 133D4 39.5

This is probably a simple quoting issue, but I can't see it at the moment.

All help appreciated.
Thanks
Alan
 
Stand down stand down...I finally managed to do something for myself.

Just removed the extra single quotes and it worked.

From this;
sed 's/IT412 587XX 7/IT412 587XX '"$V587XX"'/'

To this;
sed 's/IT412 587XX 7/IT412 587XX "$V587XX"/'

I'm sure I tried every combination...obviously not.
Cheers
 
I spoke too soon...thats not it at all...
I have looked at this problem for too long this morning...
I need more coffee
 
Hi

Personally I have no idea how to help you based on the fragments you posted. I would need a functional script which reproduces the problem. What you posted so far, works for me. ( Of course, excepting the 4 Feb 10 5:32 code. )

Feherke.
 
Hi Feherke,

You are probably right, I have been dealing with so many issues this morning that I've confused myself.

Essentially I am trying to pass a variable, within a script, to a SED command to change the value from a percentage figure, in this case 7, to an actual number.

Hence;
IT412 587XX 7

I want to change this column to represent the number.

i.e it should be
IT412 587XX 721

The SED command I tried works from the command line but not within the script itself. This is what is confusing the hell out of me.
Thanks for the reply. I'm still trying.
Cheers
 
OK, I understand now.

There is a moral here. When changing a script written by somebody else check how they have formatted the file beforehand.
They used spaces, my sed is looking for tabs.
It works now.

Thanks for the time Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top