Results 1 to 8 of 8

Thread: bash capture output between two ""

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    bash capture output between two ""

    Hi all.

    I have some output as follows
    Code:
    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.

  2. #2
    WasMeHere is offline Iced Almond Soy Ubuntu, No Foam
    Join Date
    May 2008
    Location
    Sverige
    Beans
    1,133

    Re: bash capture output between two ""

    I think it works if you pipe your output through
    Code:
    sed s/.*\ \"//|sed s/\"$//
    Have fun
    Olle

  3. #3
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    awk

    Another way is to pipe it through awk while setting the Output Field Separator to a double quote (")

    Code:
    yourscript | awk -F '"' '{print $2}'
    -F sets the field separator, $2 is the second column

    Obviously there are limitations to this method.

  4. #4
    Join Date
    Apr 2006
    Beans
    64
    Distro
    Xubuntu

    Re: bash capture output between two ""

    Another sed:

    Code:
    #> echo 'SYSTEM    "2M Hotel"' | sed "s/.*\"\(.*\)\"/\1/g"
    2M Hotel
    #>

  5. #5
    Join Date
    Jun 2006
    Location
    Antarctica
    Beans
    500
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: bash capture output between two ""

    Another sed, which avoid parentheses:
    Code:
    yourscript | sed -e 's/[^"]*"//' -e 's/".*//'

  6. #6
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash capture output between two ""

    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.

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: bash capture output between two ""

    Quote Originally Posted by Drenriza View Post
    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/

  8. #8
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    regular expressions

    Quote Originally Posted by Drenriza View Post
    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/manu...pressions.html

    http://www.faqs.org/docs/abs/HTML/regexp.html
    Last edited by Lars Noodén; October 29th, 2011 at 09:26 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •