LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-06-2008, 10:49 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 194

Rep: Reputation: 30
sed replacing line confusion


Hi,

I have the following lines in a bash script:
Code:
sudo sed -i '19s/.*/'"\t\t\thardware ethernet $MAC_ADDR;/" $DHCPD_CONF
sudo sed -i '26s/.*/'"\t\t\t\"$TFTP_ROOT_PATH\/targets\/cb6\";/" DHCPD_CONF
The first line works but not the second, I get the following error for the second:
Code:
/bin/sed: -e expression #1, char 16: unknown option to `s'
I can't figure out why. Anyone?

Thanks,
Indy
 
Old 08-06-2008, 12:17 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 299

Rep: Reputation: 115Reputation: 115
You will get an error if the variable $TFTP_ROOT_PATH contains any unescaped '/'
If so, try using a different delimiter for the sed s command e.g.
s# # # instead of s/ / /
or escape all the '/' in $TFTP_ROOT_PATH before using it in sed:-
Code:
TFTP_ROOT_PATH=/path/to/dir
TFTP_ROOT_PATH_esc=${TFTP_ROOT_PATH//\//\\/}
echo $TFTP_ROOT_PATH_esc
\/path\/to\/dir
 
Old 08-06-2008, 11:31 PM   #3
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: fedora
Posts: 196

Rep: Reputation: 32
Quote:
Originally Posted by Kenhelm View Post
Code:
TFTP_ROOT_PATH_esc=${TFTP_ROOT_PATH//\//\\/}
hi,

I am almost new to linux. can u plz explain me that line.

thx
 
Old 08-06-2008, 11:36 PM   #4
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,068

Rep: Reputation: 41
Quote:
Originally Posted by chakka.lokesh View Post
hi,

I am almost new to linux. can u plz explain me that line.

thx
That is a variable, He is using a system variable with special charaters and makeing another variable without them so that sed can read it, since sed wants special charters to be proceeded with a "\".

I hope that didnt add more confusion to your question
 
Old 08-07-2008, 03:14 AM   #5
freeindy
Member
 
Registered: Nov 2002
Posts: 194

Original Poster
Rep: Reputation: 30
thanks lads. changing the variable helped.
I see the confusion on my part now. I was dealing with $ sign but actually forgot that it is replaced when sed is dealing with it.

However, this line:
Quote:
TFTP_ROOT_PATH_esc=${TFTP_ROOT_PATH//\//\\/}
What does it actually do? Say if my variable $VAR has following data:
echo $VAR
/usr/local/share/images

What are all these slashes doing? Is there a description? Just curious.

Indy
 
Old 08-07-2008, 03:22 AM   #6
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
It escapes the forward slash: / -> \/

You could have tried it out yourself in the console. That's the best way to learn.
 
Old 08-07-2008, 03:49 AM   #7
freeindy
Member
 
Registered: Nov 2002
Posts: 194

Original Poster
Rep: Reputation: 30
jschiwal,

I get that, but why so many //\//\\/? I mean, it must have some logic somewhere.
If it would have been $VAR_esc=${VAR/\/} I could have understood say it is taking / -> \/ but this is not the case. so why '//\//\\/'?

indy
 
Old 08-07-2008, 03:55 AM   #8
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Look up parameter expansion in the bash manpage.
 
Old 08-07-2008, 04:17 AM   #9
freeindy
Member
 
Registered: Nov 2002
Posts: 194

Original Poster
Rep: Reputation: 30
ok, got it.
thanks
 
Old 08-07-2008, 04:25 AM   #10
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
You might be interested in downloading the "Advanced Bash Scripting Guide" from the www.tldp.org website. It consists entirely of commented examples that you can try yourself, making it easier to learn even though it covers advanced subjects.
 
Old 08-07-2008, 04:34 AM   #11
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: fedora
Posts: 196

Rep: Reputation: 32
Quote:
Originally Posted by trey85stang View Post
I hope that didnt add more confusion to your question
sorry it didn't.

the thing is he added the slashes at the end of the variable. How this changed the contents inside the variable
 
Old 08-07-2008, 04:36 AM   #12
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: fedora
Posts: 196

Rep: Reputation: 32
Quote:
Originally Posted by burschik View Post
Look up parameter expansion in the bash manpage.
trying....

may be related to ${parameter/pattern/string}

will read it and will be back....

Last edited by chakka.lokesh; 08-07-2008 at 04:40 AM.
 
Old 08-07-2008, 04:49 AM   #13
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: fedora
Posts: 196

Rep: Reputation: 32
seems to be got it.

I will explain it here......

after TFTP_ROOT_PATH

1. / indicates the beginning of the pattern
2. / indicates that all the patterns should be replaced
3. \/ will get evaluated to \
4. / indicates the end of the pattern
5. \\/ will become \/ which is the string.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed confusion vkmgeek Linux - Newbie 6 07-02-2008 04:50 PM
replacing sed -s option i.you Linux - Software 3 10-28-2007 12:08 PM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
sed help, replacing a letter with a word GridX Linux - Newbie 1 09-24-2003 10:21 AM


All times are GMT -5. The time now is 03:50 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration