Awk consider double quoted string as one token and ignore space in between?

Yes, this can be done nicely in awk. It's easy to get all the fields without any serious hacks.

Up vote 2 down vote favorite share g+ share fb share tw.

Data file - data. Txt: ABC "I am ABC" 35 DESC DEF "I am not ABC" 42 DESC cat data. Txt | awk '{print $2}' will result the "I" instead of the string being quoted How to make awk so that it ignore the space within the quote and think that it is one single token?

Bash unix awk link|improve this question edited Jul 8 '11 at 3:36 asked Jul 8 '11 at 3:12Roy Chan4261821 66% accept rate.

Yes, this can be done nicely in awk. It's easy to get all the fields without any serious hacks. (This example works in both The One True Awk and in gawk.

) { split($0, a, "\"") $2 = a2 $3 = $(NF - 1) $4 = $NF print "and the fields are ", $1, "+", $2, "+", $3, "+", $4 }.

To format for a one liner: cat data. Txt | awk 'split($0, a, "\"") {$2 = a2} {$3 = $(NF - 1)} {$4 = $NF} {print "and the fields are ", $1, "+", $2, "+", $3, "+", $4}' – Chris Gregg Jul 8 '11 at 4:21.

Try this: $ cat data. Txt | awk -F\" '{print $2}' I am ABC I am not ABC.

I should note that this isn't particularly generic -- it simply changes the field separator to `" and selects the second field. – Chris Gregg Jul 8 '11 at 3:27 But if I want to use the information before and after... it won't work =( – Roy Chan Jul 8 '11 at 3:35 @Roy Chan -- true. Awk is not really the right tool for parsing quoted strings.

Go down to the third post at this horribly formatted Google Cache link and you can see an example that is much longer but might help. – Chris Gregg Jul 8 '11 at 3:39 1 It's not so hard. – DigitalRoss Jul 8 '11 at 4:04 1 @DigitalRoss -- nice solution; I hadn't thought of that method.

– Chris Gregg Jul 8 '11 at 4:08.

Okay, if you really want all three fields, you can get them, but it takes a lot of piping: $ cat data. Txt | awk -F\" '{print $1 "," $2 "," $3}' | awk -F' ,' '{print $1 "," $2}' | awk -F', ' '{print $1 "," $2}' | awk -F, '{print $1 "," $2 "," $3}' ABC,I am ABC,35 DEF,I am not ABC,42 By the last pipe you've got all three fields to do whatever you'd like with.

Actually, there are 4 fields. – DigitalRoss Jul 8 '11 at 4:04 Oops -- I missed that in the original submission. – Chris Gregg Jul 8 '11 at 4:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions