LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2012, 06:15 AM   #1
qweeak
LQ Newbie
 
Registered: Jan 2012
Posts: 24

Rep: Reputation: 2
Sed not accepting variable in line replacement


Hi Guys,

I'm doing a line replacement operation and sed is not accepting any variable. This is my script
----
i=10.10.10.1
sed '5 c\IPADDR=$i\' test
----

I tried a-lot of variation like using "". None is working. Any suggestions guys
 
Old 04-01-2012, 07:11 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Try:
Code:
sed "3 c\IPADDR=$i\\" test
You need to escape the closing backslash.

Last edited by sycamorex; 04-01-2012 at 07:25 AM.
 
Old 04-01-2012, 07:22 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Double quotes are required and the . means any single character so you want to escape it if you exactly want to match the IP address. Starting with the simpler case of not addressing a specific line and substituting the match, does this work:
Code:
i='10\.10\.10\.1'
sed "s/IPADDR=$i/XXX/" test
 
1 members found this post helpful.
Old 04-01-2012, 07:36 AM   #4
qweeak
LQ Newbie
 
Registered: Jan 2012
Posts: 24

Original Poster
Rep: Reputation: 2
Hi Guys,

Thanks sycamorex. That did the trick. Can you tell how it is escaped here.
 
Old 04-01-2012, 07:46 AM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by qweeak View Post
Hi Guys,

Thanks sycamorex. That did the trick. Can you tell how it is escaped here.
In order to evaluate any variable you need to enclose the whole expression in double quotes. If you do it, any backslash will be interpreted as an attempt to escape the character following it so in your original string

Code:
$i\"
the backslash's function was to escape the closing double quote (not what we want) and the sed was looking for a closing backslash

Code:
c\......\
If we escape the backslash

Code:
\\"
the first backslash will escape the second so the second one will be treated literally as a backslash, which is what sed/regex was looking for to evaluate the whole expression.

I hope that makes sense to you
 
Old 04-01-2012, 08:07 AM   #6
qweeak
LQ Newbie
 
Registered: Jan 2012
Posts: 24

Original Poster
Rep: Reputation: 2
Hello,

Wow.. i never though of that. That makes sense. Thanks sycamorex.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
SED replace specyific line with variable zwierzq Linux - Software 4 01-23-2010 09:23 PM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
using sed to append a variable after a certain line jadeddog Programming 6 11-05-2008 12:49 AM
Pass a variable as a line number in sed dx0r515t Programming 7 03-30-2007 04:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration