Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing SED variable 2

Status
Not open for further replies.

SteveR77

Programmer
Sep 18, 2000
813
US
This is one of those things that always seems to bite me.

I have the following in my script -
sed -i '284s/"[^"]*"/"mclane.com mclane.mclaneco.com"/g' <filename>

I need to configure the command so the 284 line number can be read as $ln in case the file
gets modified and the line number that actually needs to be edited changes.

I thought the following would work but that is not the case.

sed -i "$ln s/"[^"]*"/"mclane.com mclane.mclaneco.com"/g" <filename>


Thank you for your insight and assistance.

SteveR77

I.T. where a world of choas and confusion comes down to nothing but 1s and 0s.
 
Hi

That would be correct for Sed, however the shell will probably yell at you because of unterminated string :
[pre]sed -i [highlight #8AE234]"$ln s/"[/highlight][^[highlight #8AE234]"]*"[/highlight]/[highlight #8AE234]"mclane.com mclane.mclaneco.com"[/highlight]/g[highlight #8AE234]" <filename>[/highlight] [highlight #8AE234] [/highlight] [highlight #8AE234] [/highlight] [highlight #8AE234] [/highlight][/pre]

You can either :
Code:
[gray]# escape internal double quotes[/gray]
sed -i "$ln s/\"[^\"]*\"/\"mclane.com mclane.mclaneco.com\"/g" <filename>

[gray]# double quote ( or not quote ) only the variable[/gray]
sed -i "$ln"' s/"[^"]*"/"mclane.com mclane.mclaneco.com"/g' <filename>
sed -i $ln' s/"[^"]*"/"mclane.com mclane.mclaneco.com"/g' <filename>

Feherke.
feherke.ga
 
Feherke,

Thank you. The single, double quoting, triple single quotes is always fun to attempt to keep straight.

I went with double quoting the variable and then using the single quotes which are more sed "standard" for syntax.

Thank you once again.

SteveR77

I.T. where a world of choas and confusion comes down to nothing but 1s and 0s.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top