LinuxQuestions.org
Visit Jeremy's Blog.
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 02-12-2012, 10:17 AM   #1
Ari.G
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
Smile [bash] can't remove a line from a variable using sed


Hello!
I have a a small BASH problem...
I want to remove a hole line that contains a string (the string is in a variable) from a second variable, which contains multiple lines... but I can't make it work.
example:
Quote:
sed -e /"$string"/d $text > "$text"
anyone has an idea of what to do?
thanks! =]
 
Old 02-12-2012, 11:04 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Code:
c@CW8:~$ string=foo
c@CW8:~$ lines='bar
> foo
> rab'
c@CW8:~$ edited_lines=$(echo "$lines" | sed "/$string/ d")
c@CW8:~$ echo "$edited_lines"
bar
rab
 
1 members found this post helpful.
Old 02-12-2012, 11:14 AM   #3
Ari.G
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks catkin!!!
 
Old 02-13-2012, 10:01 AM   #4
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,334

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Code:
sed -e /"$string"/d $text > "$text"
This is unclear to me. sed operates on files when this syntax is used. So does the $text variable hold the name of a file that contains the text, or the actual text itself? If the former, then you cannot use the same file for both input and output. You usually end up with an empty file, as the ">" redirection is processed first, overwriting the contents before the rest of the command is run. You have to work through a temporary file. gnu sed does have a -i option for writing in place, though.


If the variable is intended to contain the text itself, as your description appears to state, then not only can you not use this syntax (you have to feed the contents into sed's stdin in some way), but you also can't use ">" to redirect stdout into a variable. The variable contents will be treated as a filename to use. You have to use some kind of command substitution, or read, or a similar technique, to store the stdout of a command inside a variable.

Finally, you also need to enclose the entire sed expression in double-quotes, so that it's passed to it as a single argument.

Catkin's example shows you how it's done.


By the way, when modifying the contents of a variable, you can often use simple built-in parameter substitution instead of external tools.

Code:
$ text='line one
+ line two
+ line three'

$ echo "$text"
line one
line two
line three

$ string='line two'		#the line to be removed

$ echo "${text/$string}"	#removes the text, but not the newline character
line one

line three

$ echo "${text/$string$'\n'}"	#also remove the newline
line one
line three
Notice how you can use ansi-c style quoting ($'', see the quoting section of the bash man page) to generate a newline. You may need to enable shopt -s extquote first if you use it in a non-interactive script, or else include the newline directly in the string variable.

Code:
$ string=$'line two\n'

$ echo "${text/$string}"
line one
line three

Finally, I also want to mention that, depending on the content (e.g. if the variable contains a list of filenames), it might possibly be better to use an array instead, with one line per entry, rather than a single variable with multiple lines.

http://mywiki.wooledge.org/BashFAQ/005
 
  


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
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
Remove line containing certain variable? [Bash] alancampbell6 Programming 10 07-20-2011 08:43 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 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


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