Solved

ENV vars in SED patterns

Posted on 1998-04-03
8
663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

634 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