LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-03-2011, 10:58 PM   #1
RocketJumper
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
SED with variables


Hello everyone, I have this problem below.

Quote:
b="./list"
echo './list ./out' | sed "/$b/d"
Which comes out as an error.
Quote:
sed: -e expression #1, char 5: extra characters after command
Anyone have any idea how this can this solved?

Last edited by RocketJumper; 02-03-2011 at 11:01 PM.
 
Old 02-04-2011, 12:08 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have a look at your delimiter in sed, it is a slash (/). Now have a look at the string being passed in:
Code:
b="./list"
If you combine this altogether then it looks like (once expanded):
Code:
echo './list ./out' | sed "/./list/d"
If your ever not sure or cannot see the issue plainly, try doing the following:
Code:
echo './list ./out' | echo sed "/$b/d"
 
Old 02-04-2011, 12:37 AM   #3
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Greetingz!

Yes, grasshopper, you must master shell escapes, or forever STDERR will dominate you.
Code:
b="./list"
echo './list ./out' | sed "/\$b/d"
Remember to give thanks when earned, and mark any solved threads as such via the "Thread Tools"

Nevermind!

Last edited by xeleema; 02-04-2011 at 04:41 AM. Reason: Im a dork.
 
Old 02-04-2011, 03:25 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@xeleema - sorry to be the bearer of bads new but escaping the $ sign is causing only lines with '$b' to be removed. Again the echo addition would yield:
Code:
$ echo './list ./out' | echo sed "/\$b/d"
sed /$b/d
Which I am guessing will still not help OP to remove this line.

Personally I am not sure there is a solution based on this scenario as the escape would actually need to look like the following, assuming no variable being used:
Code:
echo './list ./out' | sed "/\.\/list/d"
So both the dot (.) and the slash (/) need to be escaped to get an accurate removal.
 
1 members found this post helpful.
Old 02-04-2011, 04:42 AM   #5
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
@grail
Hey, but at least I got past the error. In my defense, OP never said what it was they were trying to actually accomplish. :P
So is there any way to feed sed a variable and have it act on it? I'm kinda curious about that myself.

Basically, if the value of b is unknown, how can we write a sed statement that will delete b from the string?

Last edited by xeleema; 02-04-2011 at 04:43 AM.
 
Old 02-04-2011, 06:50 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am guessing, as we still need OP to come back, that the idea initially was to simply use a variable in a sed statement. I think that maybe the unintentional fact that the value of this particular
variable is causing so much angst was not the initial intention.

Guess we will wait and see what they say
 
Old 02-04-2011, 08:33 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by grail View Post
Personally I am not sure there is a solution based on this scenario as the escape would actually need to look like the following, assuming no variable being used:
Code:
echo './list ./out' | sed "/\.\/list/d"
So both the dot (.) and the slash (/) need to be escaped to get an accurate removal.
Well, there is but it's (surprise) ugly. Generally, for using variables which contain a delimiter
Code:
b="./list"
echo './list ./out' | sed "/${b////\/}/d"
Better: Also escaping the 3. slash. Though not necessary in the above case. However, NOT escaping it will cause trouble when handling the '.'.
So finally
Code:
b="./list"
echo './list ./out' | echo sed "/${b//.\//\.\/}/d"
However, this technique also has it's limitations, e.g., when there are too many characters that would have to be escaped in order to not be interpreted as RegEx.
 
Old 02-04-2011, 08:52 AM   #8
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
It might be prettier to escape the search string in the first place:

Code:
export b="\.\/list" # ./list
echo './list ./out'| sed "s/${b}.//"
search and replace by nothing (=effectively removal)
 
Old 02-04-2011, 09:07 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Ramurd View Post
It might be prettier to escape the search string in the first place:

Code:
export b="\.\/list" # ./list
echo './list ./out'| sed "s/${b}.//"
search and replace by nothing (=effectively removal)
Well, yes, but sometimes you do not know in advance how the variable looks like. And sometimes when you read a text-file you do not want to change it's contents. In those cases escaping the delimiter is a good choice.
 
Old 02-04-2011, 06:47 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Maybe this is an example of using the wrong tool for the job. Perl has the quotemeta() function to handle cases just like this.
--- rod.
 
Old 02-05-2011, 02:28 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am with rod on this one. Whilst methods have been shown to solve the problem, I can easily see issues arising from both methods.

As usual, the OP will need to explain what there requirement is a bit more as we may be solving issues that are not really there but that the chosen
example made it particularly complicated
 
Old 02-06-2011, 08:31 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Just use a delimiter other than '/'?
 
Old 02-06-2011, 09:09 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@MTK358 - to the best of my knowledge your suggestion is only good when being used with the replace option. Happy for you to correct me if I am wrong?
 
Old 02-06-2011, 09:20 AM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by grail View Post
@MTK358 - to the best of my knowledge your suggestion is only good when being used with the replace option. Happy for you to correct me if I am wrong?
I didn't notice that it wasn't a replacement. There probably is a way to use a different delimiter, but I'm not familiar enough with sed to know.

You can also escape the slashes, the advantage of that is that the filename can contain any characters you want.

Code:
b=./list
b=$(echo "$b" | sed 's#/#\\/#g')
echo './list ./out' | sed "/$b/d"

Last edited by MTK358; 02-06-2011 at 09:23 AM.
 
Old 02-06-2011, 09:49 AM   #15
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MTK358 View Post
You can also escape the slashes, the advantage of that is that the filename can contain any characters you want.
Code:
b=./list
b=$(echo "$b" | sed 's#/#\\/#g')
echo './list ./out' | sed "/$b/d"
That is what the bash substitution is for in my earlier post
Code:
echo './list ./out' | sed "/${b////\/}/d"
When dealing with variables it is good practice to escape the delimiter if the content is unknown.
Quote:
There probably is a way to use a different delimiter, but I'm not familiar enough with sed to know.
There is
Code:
sed '\#pattern# d' file
But as it has already been stated, it is always problematic when the variable contains characters which can be interpreted as part of a RegEx.

Last edited by crts; 02-06-2011 at 09:50 AM.
 
  


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
Sed and variables oden Programming 9 07-29-2007 04:36 PM
sed and bash variables cashton2k Programming 1 03-23-2007 04:43 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
passing variables to sed jjfate Programming 8 07-31-2003 04:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 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