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
Solved

ENV vars in SED patterns

Posted on 1998-04-03
8
659 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
EMC VNX  storage pool  Vs  raid group  question 3 137
FTP on FreeBSD server 2 139
How to clone solaris 10 machine 33 146
Linux hostname change 2 83
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…
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 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…

790 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