Solved

ENV vars in SED patterns

Posted on 1998-04-03
8
651 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

895 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

14 Experts available now in Live!

Get 1:1 Help Now