Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delimited by space and double quotes

Status
Not open for further replies.

jdempsey069

Technical User
Nov 22, 2002
4
GB
I have a file which has text enclosed in double quotes, but numbers and dates un-quoted. How can I query the number of fields but indicating the correct delimiter.

eg. "1234X" 0 "" " " 07/10/05 ? "Today" 0 0

has 9 fields.
thanks
 
Hi

I hate such format.
Code:
BEGIN {
  FS=""
}

{
  nf=1
  q=0
  for (i=1;i<=NF;i++) {
    if ($i=="\"") q=1-q;
    if ($i==" " && !q) nf++
  }
  print nf
}
This simply print the proper number of fields, as you requested. I would change it's format as soon as possible, to something easier to parse.

Feherke.
 
Feherke,

running this against my example, I have 1 returned. Is there something missing?

thanks
john
 
Hi

Strange. In fact, I use [tt]gawk[/tt] 3.1.1, with a standard [tt]awk[/tt] probably will not work. If you have no [tt]gawk[/tt], try it with [tt]nawk[/tt], [tt]mawk[/tt] or any extended variant.
Code:
[blue]master #[/blue] echo '"1234X" 0 "" " " 07/10/05 ? "Today" 0 0' | quoted.awk
9

Feherke.
 
A starting point:
awk '{x=$0;gsub(/"[^"]*"/,"X",x);print "Record "NR" has "split(x,a)" fields"}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
If I do not specify a delimiter, HP-UX awk (standard) reports "cannot have more than 199 fields". I know it should only contain max 191 fields, which is what I'm trying to check.

I'll also look at getting another version in the meantime.

thanks
 
awk -F'|' '{x=$0;gsub(/"[^"]*"/,"X",x);print "Record "NR" has "split(x,a,/[ \t]+/)" fields"}'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV

that worked ok, thanks.
Could you just explain what the gsub is doing, to help my learning.

john
 
It replace this:
"1234X" 0 "" " " 07/10/05 ? "Today" 0 0
By this:
X 0 X X 07/10/05 ? X 0 0

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top