Find the answer to your Linux question:
Results 1 to 4 of 4
I'm having problems extracting data between single-quotes using Sed. For example, If I have the following lines: var a = 'hello' a = 'hi, there' var foo = 'goodbye' The ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    4

    Extracting data between single-quotes using Sed

    I'm having problems extracting data between single-quotes using Sed.

    For example, If I have the following lines:

    var a = 'hello'
    a = 'hi, there'
    var foo = 'goodbye'

    The expected output I'm trying to achieve would be:

    hello
    hi, there
    goodbye

    If anyone could help me out that would be greatly appreciated. Thanks.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So the way to do this is pattern matching with regular expressions. One sed command would be:
    Code:
    s/^.*'\(.*\)'.*$/\1/
    What does this do? Well, anything in the format s/PATTERN/REPLACEMENT/ finds the first occurrence of PATTERN, and replaces it with REPLACEMENT. Easy, huh?

    So what pattern did I use?

    ^ - Match the beginning of the string
    .* - 0 or more of any character
    ' - A literal quote
    (.*) - 0 or more of any character AND REMEMBER THEM (this is what the parentheses do)
    ' - Another literal quote
    .* - 0 or more of any character (again)
    $ - Match the end of the string

    The replacement that I used is \1. This takes the first remembered string from the pattern and uses that.

    Do you see how this works? We replace an entire line with whatever was between single quotes.

    Now, this is not perfect. It assume that you only use single quotes and not double quotes. It also assumes that there are only two single quotes on each line. But as a simple regex, I think it will work.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    4
    Cabhan,

    Your detailed explanation of your solution has been very helpful in helping me solve my problem and helping me understand regular expressions a little more.

    Thank you!

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    your data looks structured and consistent. All the words you want to get is after the "=". ie the 2nd field is we put "=" as the delimiter. It would be easier to work with fields
    Code:
    awk -F "=" '{ 
      print "Before: "$2
      gsub("\047","",$2)
      print "After: "$2
    }' file

Posting Permissions

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