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!

"has too many fields" error 1

Status
Not open for further replies.

djbjr

Programmer
Dec 7, 2001
106
US
Hi folks, Im new to AWK but I have about 150 files that I need to pull about a dozen fields from these comma delimeted files.

When I run the AWK script I wrote I am getting a "has too many fields" error.

The file has a lot of columns (more than 100 columns)

Can any of you experts let me know what the maximum fields allowed in a file is? I edited someone else AWK script so I am pretty sure my script is ok. I'll paste it below.

Any suggestions will be greatly appreciated.

thanks


echo "Please enter the name of the Availability file: \c"
read filename
if [ -f $filename ]
then echo "....accessing file......"
else echo "file doesnt exist"
fi
echo "Please enter the delimiter: \c"
read delimiter
awk -F$delimiter '{print $1, $2, $76, $77, $78, $79, $80, $81, $94, $95, $96, $97, $98, $99}' $filename > pu.data
 
Usually 100 fields and 3000 characters per record for nawk.
You may try gawk
Or perhaps simply cut

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
gawk didnt work so I tried NAWK and that seemed to work.

The only problem is that it looks like I have some columns within the fields themselves which are enclosed with double quotes.

Is there a way to handle that? Perhaps I can ask the folks providing the file to change the delimiter.

I have two more hurdles that I have to over come.

The first row of the file has 2 values that I need to add as the first two columns of the file.

example --

File looks like this
2005 002
value1, value2, value3, value4
value1, value2, value3, value4
value1, value2, value3, value4


End result to look like this
2005, 002, value1, value2, value3, value4
2005, 002, value1, value2, value3, value4
2005, 002, value1, value2, value3, value4

Also I need to figure out how to grab all the files in the folder. Right now I wrote it to accept a file name entered at a prompt. I suppose I can just generate it via EXCEL functions but it would be nice if I could do that too.




 
nawk -F$delimiter '
BEGIN{OFS="'$delimiter' "}
NR==1{pref=$0;sub(/ /,OFS,pref;next)
{print pref, $1, $2, $76, $77, $78, $79, $80, $81, $94, $95, $96, $97, $98, $99}
' $filename > pu.data

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks but I got the following error:

nawk: syntax error at source line 3
context is
NR==1{pref=$0;sub(/ >>> /,OFS,pref; <<<
nawk: illegal statement at source line 3
nawk: syntax error at source line 5
missing }


I solved all my other problems by generating the script via an excel function. I assume te above was to help with the , within the quoted fields?
 
Sorry for the typos:
nawk -F$delimiter '
BEGIN{OFS="'$delimiter' "}
NR==1{pref=$0;sub(/ /,OFS,pref);next}
{print pref, $1, $2, $76, $77, $78, $79, $80, $81, $94, $95, $96, $97, $98, $99}
' $filename > pu.data

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

It brings the first row and adds them as the first 2 columns of the file. THANKS.

I dont know if you meant it to handle the , within the double quotes because that is still not working.

You are awesome by the way!
 
if you meant it to handle the , within the double quotes
No provision for this issue in the posted code :-(

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks I have bothered you enough, I will try to find the answer somewere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top