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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk: counting (large?) file rows and columns

Status
Not open for further replies.

Toby2007

Technical User
Nov 1, 2007
4
GB
Hi,

I'm a newbie to the forum & linux scripting in general, so i hope i'm posting the right place.

Problem: I have written a one-liner script to count the # of rows and coulmns for a file, like this:
% more dim
if [ $# -eq 0 ]
then
echo "Usage: dim filename"
exit
fi
awk 'BEGIN{FS="\t"} {nfe=NF} END{print NR " " nfe}' $1

This works fine on small files. e.g.

% dim smallfile.ped
2300 3481

However, it does not appear to work for larger files. e.g. for an example file with 2300 rows and 20924 columns ("largefile.ped") i obtain the following:
% dim largefile.ped
2301 0

Could anyone suggest what is going wrong here? It appears to me that awk is counting an extra line & therefore the built-in variable NF counts zero fields for this empty record. However, i know the file has 2300 records, not 2301 - i have checked usiing vi!

Any advise would be appreciated,

Toby
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top