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 ...
- 09-16-2008 #1Just 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.
- 09-17-2008 #2
So the way to do this is pattern matching with regular expressions. One sed command would be:
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?Code:s/^.*'\(.*\)'.*$/\1/
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
- 09-17-2008 #3Just 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!
- 09-18-2008 #4Linux 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


Reply With Quote