View Full Version : bash capture output between two ""
Drenriza
October 28th, 2011, 02:18 PM
Hi all.
I have some output as follows
SYSTEM "2M Hotel"
How can i capture the text between the two "" so the only output i get is 2M Hotel ?
Thanks on advance.
Kind regards.
WasMeHere
October 28th, 2011, 02:29 PM
I think it works if you pipe your output through
sed s/.*\ \"//|sed s/\"$//
Have fun
Olle
Lars Noodén
October 28th, 2011, 02:36 PM
Another way is to pipe it through awk (http://manpages.ubuntu.com/manpages/oneiric/en/man1/awk.1posix.html) while setting the Output Field Separator to a double quote (")
yourscript | awk -F '"' '{print $2}'
-F sets the field separator, $2 is the second column
Obviously there are limitations to this method.
tors
October 28th, 2011, 02:39 PM
Another sed:
#> echo 'SYSTEM "2M Hotel"' | sed "s/.*\"\(.*\)\"/\1/g"
2M Hotel
#>
dargaud
October 28th, 2011, 03:42 PM
Another sed, which avoid parentheses:
yourscript | sed -e 's/[^"]*"//' -e 's/".*//'
Drenriza
October 28th, 2011, 09:48 PM
Thanks for all the suggestions guys. Wish i knew what all those characters in the sed commands ment. But it looks a bit exhausting :) to be honest.
ofnuts
October 28th, 2011, 09:59 PM
Thanks for all the suggestions guys. Wish i knew what all those characters in the sed commands ment. But it looks a bit exhausting :) to be honest.These are "regular expressions" and any programmer worth his salt should know about them.
http://xkcd.com/208/
Lars Noodén
October 29th, 2011, 09:23 AM
Thanks for all the suggestions guys. Wish i knew what all those characters in the sed commands ment. But it looks a bit exhausting :) to be honest.
They are not just for programmers, they're essential also to system administration.
There are lots of sources of material on regular expressions. For sed you can start here:
http://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
http://www.faqs.org/docs/abs/HTML/regexp.html
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.