LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
Search this Thread
Old 10-29-2004, 05:30 AM   #1
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
sed not working if value is passed thru a variable containg value


sed -n '/"$line"/,/REPORT/p' ~/DCS/great1

line containg some value
line contains 11:00 12:00

if instead of $line i write the value it works ..

plz help


does not give error also.. does not find..
 
Old 10-29-2004, 05:43 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Code:
sed -n '/'"$line"'/,/REPORT/p' ~/DCS/great1
should work.
 
1 members found this post helpful.
Old 10-29-2004, 05:46 AM   #3
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
it worked

can u explain why and how it worked..
where i was mistaken
 
Old 10-29-2004, 05:54 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
single quotes prevent shell variables expansion.
I put your variable outside the single quotes.
 
Old 10-29-2004, 06:04 AM   #5
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
why do we write these commands in single quotes.. they work without them also..
 
Old 10-29-2004, 07:27 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by suchi_s
why do we write these commands in single quotes.. they work without them also..
If you have the sed command without quotes, the shell will try to expand wildcards and shell/environment variables. So the shell looks in the sed command for special characters (like *[]$) to replace them according to shell expansion mechanisms. With sed, most often you do not want the shell to change your sed-command. Sed-commands often contain special regular expression characters like $[].*. If you do not quote the sed command, the shell will mess them up.

In your case, you don't use special regular expression characters specific to sed. In your case, you even do want the shell to change your sed-command (expand the variable).

When you use sed for less simple tasks (i.e. real regexp's instead of your fixed strings), which is often the case when sed is used, you do need to use single quotes, to prevent the shell from touching them.

So, it's considered a good idea to always single-quote sed-commands, and make the exception for the parts you do need the shell to process them. So jlliagre's way of quoting your sed command may look more complex then needed. But that's just your (relatively simple) case of fixed string replacement with sed. jlliagre's quoting does show the best way to make your habit when using sed: Single-quote everything, except the part you do need the shell to change.

Last edited by Hko; 10-29-2004 at 07:36 AM.
 
Old 10-29-2004, 08:07 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
One more reason to quote the sed command is that if it contains on or more spaces, which happen frequently, the shell will break it in multiple arguments to sed while it expects a single one, thus breaking sed.
Others characters that would fool sed or the shell are & ; , | < > and so on ...
 
Old 10-29-2004, 08:41 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
How could I forget that...
 
1 members found this post helpful.
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash, using variable in sed fur Programming 3 11-12-2005 08:41 AM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 08:07 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 08:54 PM
sed and variable chirophil91 Programming 2 10-19-2004 04:23 AM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 06:17 PM


All times are GMT -5. The time now is 05:06 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration