LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-14-2011, 10:39 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 577

Rep: Reputation: 30
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 ?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-14-2011, 11:28 AM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
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

Last edited by Telengard; 04-14-2011 at 11:38 AM. Reason: input redirection not needed with cut
 
1 members found this post helpful.
Old 04-14-2011, 11:32 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

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

Code:
sed 's/"\(.*\)"/\1/'
 
1 members found this post helpful.
Old 04-14-2011, 11:58 AM   #4
Medievalist
Member
 
Registered: Aug 2003
Distribution: Dead Rat
Posts: 175

Rep: Reputation: 37
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!
 
2 members found this post helpful.
Old 04-14-2011, 12:11 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by Medievalist View Post
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.
 
1 members found this post helpful.
  


Reply

Tags
awk, quotes


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can not input double quotation marks(invalid quotation key) wangzhilife Linux - Hardware 2 09-18-2009 06:44 AM
Use of quotation marks in bash dunryc Programming 2 06-08-2008 02:24 PM
Execvp With Quotation Marks amitbern Programming 10 11-25-2005 10:28 AM
no quotation marks jdoe9898 Linux - Newbie 1 10-12-2005 03:07 AM
Quotation marks and strings - in C lazyuser Programming 5 01-25-2005 08:14 AM


All times are GMT -5. The time now is 09:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration