LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Read only quotation marks text (http://www.linuxquestions.org/questions/programming-9/read-only-quotation-marks-text-875010/)

czezz 04-14-2011 10:39 AM

Read only quotation marks text
 
Here is a sample of file.
Columns are separated by SPACE. Column 5th is in quotation marks.

Code:

3/25/11 35600 N/A 26002-t "efierfr efhiuhfuier elfnehfioue" 2 1.7738223E7 6.6880178E7 8.4618401E7
3/25/11 35600 N/A 26002-t "Addfdfdf  saasdasdsa (A1332)" 1 273.0 143.0 416.0
3/25/11 35600 N/A 26002-t "Nasdeeeeee Cxdsdsdsasd  Nxx-4" 1 114.0 0.0 114.0

I want to separate column 5th form the rest.
I tried with this:
Code:

cat x | awk {'print $5'}
"efierfr
"Addfdfdf
"Nasdeeeeee

...but result is (as u see) incomplete.

How can I ask AWK to read everything between quotation marks ?

Telengard 04-14-2011 11:28 AM

Code:

foo$ awk -F '"' '{print $2}' x
efierfr efhiuhfuier elfnehfioue
Addfdfdf  saasdasdsa (A1332)
Nasdeeeeee Cxdsdsdsasd  Nxx-4
foo$

Or probably more efficiently:

Code:

foo$ cut -d '"' -f 2 x
efierfr efhiuhfuier elfnehfioue
Addfdfdf  saasdasdsa (A1332)
Nasdeeeeee Cxdsdsdsasd  Nxx-4
foo$

Other solutions certainly exist. These are merely what I came up with in context of the given parameters.

You may also find this enlightening:
http://en.wikipedia.org/wiki/Cat_%28...ess_use_of_cat

MTK358 04-14-2011 11:32 AM

If column 5 is the only one in quotation marks, this will work:

Code:

sed 's/"\(.*\)"/\1/'

Medievalist 04-14-2011 11:58 AM

Redefine awk's field separator character
 
Quote:

How can I ask AWK to read everything between quotation marks?
Code:

awk -F\" '{print $2}'
this is functionally the same as

Code:

awk 'BEGIN{FS="\""};{print $2}'
You are redefining awk's field separator to be the quotation mark. The slash mark is necessary in the first form to prevent your shell from interpreting the quote, it's necessary in the second form to show that the second quotation mark is not closing the first one.

If any input line contains no quotation marks at all this code will output a blank line.

The GNU awk is superior to all the other awks; it has the ability to do fixed-width fields and numeric base conversions. A hearty thanks to Arnold Robbins, the GNU awk maintainer!

Telengard 04-14-2011 12:11 PM

Quote:

Originally Posted by Medievalist (Post 4324692)
Code:

awk 'BEGIN{FS="\""};{print $2}'

Nice :)

Another variant:

Code:

foo$ awk -v FS=\" '{print $2}' x
efierfr efhiuhfuier elfnehfioue
Addfdfdf  saasdasdsa (A1332)
Nasdeeeeee Cxdsdsdsasd  Nxx-4
foo$

It is also possible to surround the output lines with double quotes if desired.

Code:

foo$ awk -F \" '{print "\""$2"\""}' x
"efierfr efhiuhfuier elfnehfioue"
"Addfdfdf  saasdasdsa (A1332)"
"Nasdeeeeee Cxdsdsdsasd  Nxx-4"
foo$

In a complex program one could change output separators on-the-fly.


All times are GMT -5. The time now is 08:58 AM.