Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ENV vars in SED patterns

Posted on 1998-04-03
8
Medium Priority
?
670 Views
Last Modified: 2010-08-05
In a csh script, I want to use 'sed' to substitute a placeholder pattern with user's full names (extracted from GCOS with a targeted grep/cut operation and set to variable $NAME).
The script line I have goes like this:
         sed -e 's/PUTNAME/'$NAME'/' < $LOC/.vacation.msg1 > $LOC/.vacation.msg
Problem is: 'sed' doesn't  like the space between first and last name:
         sed: command garbled: s/PUTNAME/Cal
(note how it drops my last name)
Can someone suggest a better sed string and comment on quoting variables in 'sed'?
0
Comment
Question by:cal_sawyer
  • 4
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:rickyr
ID: 2009068
Hi...
Take out the single quotes around $NAME or change them for double quotes ...
sed -e 's/PUTNAME/"$NAME"/'

The single quote is understood by sed to mean start and end of the editor commands.
That is why you get the garbled message. If you want to quote variables in sed then double quote them.

regards

0
 

Author Comment

by:cal_sawyer
ID: 2009069
Sorry.

The double quotes make the strings enclosed literal.

I'm editing a generic vacation notifier message header and get:

From: "$LOGNAME"@eciad.bc.ca ("$NAME")

So now it's literal AND I get doublequotes around the quoted vars!
0
 
LVL 85

Expert Comment

by:ozo
ID: 2009070
sed -e s/PUTNAME/$NAME/ < $LOC/.vacation.msg1 > $LOC/.vacation.msg

sed -e "s/PUTNAME/$NAME/" < $LOC/.vacation.msg1 > $LOC/.vacation.msg

sed -e 's/PUTNAME/'$NAME'/' < $LOC/.vacation.msg1 > $LOC/.vacation.msg
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:ozo
ID: 2009071
Sorry didn't read the questeion carefully enough
sed -e "s/PUTNAME/$NAME/" < $LOC/.vacation.msg1 > $LOC/.vacation.msg
sed -e s/PUTNAME/"$NAME"/ < $LOC/.vacation.msg1 > $LOC/.vacation.msg
sed -e 's/PUTNAME/'"$NAME"'/' < $LOC/.vacation.msg1 > $LOC/.vacation.msg
0
 

Author Comment

by:cal_sawyer
ID: 2009072
Bingo!  Wow, three solutions and I didn't get ONE of 'em .... back to the books, eh?

Thanks!
0
 
LVL 85

Expert Comment

by:ozo
ID: 2009073
It's not really sed who's processing the " or ' or space here.
They were being parsed by csh to determine what to pass to sed.
0
 
LVL 3

Expert Comment

by:rickyr
ID: 2009074
righty-oh
0
 
LVL 85

Accepted Solution

by:
ozo earned 400 total points
ID: 2009075
I take it you've gotten the answer you wanted?
sed -e "s/PUTNAME/$NAME/" < $LOC/.vacation.msg1 > $LOC/.vacation.msg
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question