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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bourne + sed giving trouble 1

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
I'm pretty new to this, but think I have a decent grasp on it. However, amid a slew of different attempts, the solution to this particular combination is eluding me.

I have a bourn shell script which does some stuff, one of those stuffs to execute a sed command. It's giving me all sorts of trouble.

Here's the gist...

Code:
CMD = "sed -e 's/^${FIND}.*/${FIND}${1}/' ${INFILE} > ${OUTFILE}"

$CMD
RETURN=$?
echo "Return from command $CMD is $RETURN"

Now the output for this is
Code:
Unrecognized command: 's/Searchstring.*/SearchstringAppendString/'

Return from command: sed -e 's/^Searchstring.*/SearchstringAppendString/' /tmp/test.txt > /tmp/test2.txt is 2

If I cut and paste the sed command, it runs perfectly.

I've tried many different combinations of the quotes, and escaping and it just doesn't seem to go well.

I went the route of not assigning it to a variable and just enclosing the string in backticks, that seemed to work fine, but the output file wasn't created.

Appreciate any help, thanks.


 
And what about this ?
sed -e "s/^${FIND}.*/${FIND}${1}/" ${INFILE} > ${OUTFILE}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Same as when I tried enclosing it in backticks...
RETURN = 0
and everything looks fine, but the /tmp/test2.txt field doesn't exist.
 
Have you tried set -x before the sed line ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Just tried it, very helpful (thank you) hint in general... but isn't particularly enlightening here.

It is showing that it's executing the entire sed line (the one that I can cut and paste and have work just fine).

Would this mean that sed is returning the error? Or is it running these as some kind of list of commands?

(Thanks for helping me with this, I'm feeling completely stumped)
 
can you please post what is displayed when you execute this ?
set -x
echo "'F=$FIND,1=$1,I=$INFILE,O=$OUTFILE'"
sed -e "s/^${FIND}.*/${FIND}${1}/" ${INFILE} > ${OUTFILE}
RETURN=$?
set +x

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I know that was your first suggestion... and I swear I tried it without success. But now it's rolling through just fine.

My apologies, in all my attempts I must've had some typo when trying the first time.

Thanks so much for your help and your patience.

 
As an FYI for anyone else stumbling along... the problem with encapsulating it as a variable seems to relate to the redirected output... looking at the output the command which I get by using the set -x, that's the only difference between the working and non-working versions.
 
If you're curious enough, take a look at the eval command in your shell man pages.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
did you already study the difference
between " and '

" substitutions are done
' substitutions are ignored

so what happen in the code:
CMD = "sed -e 's/^${FIND}.*/${FIND}${1}/' ${INFILE} > ${OUTFILE}"

a) all your {} are UNUSED + confusing, they just show
you missed the lesson
b) $1 is unknown by sed, if this the 1.st parameter
of the shell, it will NOT be expanded, see next
c) the ${FIND} after an ' will NOT be expanded

something like this has a better chance (not tested):

CHANGE=abc
APPEND=123
cmd="sed -e \"s/^$CHANGE/\1$APPEND/g\""
will append 123 to abc, results: abc123
if abc is on line beginn.

:) guggach
 
I was outputting CMD, and everything was expanding properly.

I also tried hard coding the CMD line as a test run, still did not work.

Works now though by just skipping the variable, and seeing as there's no need for the variable, I'm fine with that.
 
guggach,
does 'jamisar' mean anything to you?

just curious

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
but of course - the more things change......

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top