LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 09-20-2011, 08:46 PM   #1
rhuhawk
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
BASH: string manipulation


Hi, I am trying to figure out how to isolate certain parts of string.

In a given file (test.txt) I have some lines:

blah [Need this phrase] bblah
blah ya [Need
this phrase as well]

Is there a way for me to extract only what is in the square brackets? Even if the text goes to the next line?

The extracted phrase should be saved as a variable. Is there any way to do this with the sed command?
Thank you!
 
Old 09-20-2011, 09:02 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,814

Rep: Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001Reputation: 1001
Hello,

You would have to use sed for this. Look at the following:
Code:
sed -e 's/.*\[\([^]]*\)\].*/\1/g'
This would be the command line equivelant to perform the operation you are looking for. As an example (I'm not within linux, so this might be off...):
Code:
echo "blah [Need this phrase] bblah" | sed -e 's/.*\[\([^]]*\)\].*/\1/g'
Which should return for you "Need this phrase".

Cheers,

Josh
 
Old 09-20-2011, 09:12 PM   #3
rhuhawk
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Great!

Thanks for the quick reply!!!

When I run this:

sed -e 's/.*\[\([^]]*\)\].*/\1/g' test.txt

and test.txt is:
blah [Need this phrase] bblah
blah ya [Need
this phrase as well]


It outputs:
Need this phrase
blah ya [Need
this phrase as well]

Is there any way I can get it to keep reading lines? When the desired string begins on one line and continues on another line how could I get it keep reading through to the next line until it finds the end ].

Thanks again in advance

Last edited by rhuhawk; 09-20-2011 at 09:24 PM.
 
Old 09-20-2011, 11:57 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
How about:
Code:
sed -r ':a /]/! N;ta;s/.*\[(.*)\].*/\1/' file
 
Old 09-21-2011, 06:37 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
$ ruby -0777 -ne '$_.split("]").each{|x| puts "#{x.split("[")[-1]}" if x[/\[/]  }' file
Need this phrase
Need
this phrase as well
 
Old 09-21-2011, 07:30 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
So still learning from the master <bow> to kurumi

Now that i have seen what 0777 can do:
Code:
ruby -0777 -ne 'puts $_.scan(/\[([^\]]+)/)' file
 
Old 09-21-2011, 09:55 AM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Using GNU awk
Code:
echo '
blah
blah [Need this phrase] bblah
[
Need
this
phrase
]
[Need this phrase] blah ya [Need
this phrase as well] blah [Need this phrase] blah
blah' | awk '/./' RS='[^]]*[[]\n?|\n?][^[]*'

Need this phrase
Need
this
phrase
Need this phrase
Need
this phrase as well
Need this phrase
Or, to have each phrase on a single line
Code:
awk '/./{gsub(/\n/," ");print}' RS='[^]]*[[]\n?|\n?][^[]*'

Need this phrase
Need this phrase
Need this phrase
Need this phrase as well
Need this phrase
 
Old 09-21-2011, 10:17 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
small correction

Quote:
Originally Posted by grail View Post
How about:
Code:
sed -r ':a /]/! N;ta;s/.*\[(.*)\].*/\1/' file
The 't' command will only jump if an 's' command has made a substitution since the last line was read. So a conditional 't' jump directly after reading a new line has no effect.

This works as long as there are no multiple patterns on the same line to keep:
Code:
sed -r ':a /]/! N;s/.*\[(.*)\].*/\1/;Ta' file
 
Old 09-21-2011, 10:37 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Cheers crts ... still getting my sedfu together although I noticed with Kenhelm's example this doesn't get all the necessary ones
 
Old 09-21-2011, 12:05 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by grail View Post
Cheers crts ... still getting my sedfu together although I noticed with Kenhelm's example this doesn't get all the necessary ones
Yes, as I stated above
Quote:
This works as long as there are no multiple patterns on the same line to keep:
the solution has some restrictions. To also accommodate for Kenhelm's sample data we could use:
Code:
sed -nr ':a /\[[^]]*$/ {N;ba}; s/[^[]*\[([^]]*)\][^[]*/\1/pg; ' file
As you can see, with the above solution we have to use an unconditional jump.
 
  


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 string manipulation help ptml Linux - Newbie 8 08-26-2010 07:38 PM
String manipulation in bash dalmat Linux - Software 4 12-03-2007 06:50 PM
bash string manipulation kpachopoulos Programming 3 06-17-2007 07:45 PM
string manipulation in BASH ovince Programming 4 04-16-2007 08:15 PM
bash + string manipulation dave bean Programming 7 02-16-2005 12:16 PM


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