Solved

ENV vars in SED patterns

Posted on 1998-04-03
8
645 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 84

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
 
LVL 84

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 84

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 84

Accepted Solution

by:
ozo earned 100 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now