count lines of file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

count lines of file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-06-2007
panknil panknil is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 19 February 2013, 12:36 PM EST
Location: Kolkata
Posts: 90
Thanks: 2
Thanked 0 Times in 0 Posts
Question count lines of file

dear all,
i want to count the lines of a flat(text) file using awk.i have tried with {print NR}
but its taking lot of time for a big file like 2GB file size.
so i want better efficiency...so can any body please help me with some other and better awk code?

Regards,
Pankaj
Sponsored Links
    #2  
Old 06-06-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 24 April 2014, 1:24 PM EDT
Location: Foxborough, MA
Posts: 7,541
Thanks: 137
Thanked 539 Times in 506 Posts
Quote:
Originally Posted by panknil
dear all,
i want to count the lines of a flat(text) file using awk.i have tried with {print NR}
but its taking lot of time for a big file like 2GB file size.
so i want better efficiency...so can any body please help me with some other and better awk code?

Regards,
Pankaj
why 'awk'?
wouldn't 'wc -l' be sufficient?
Sponsored Links
    #3  
Old 06-06-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 11 April 2014, 1:02 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by panknil
dear all,
i want to count the lines of a flat(text) file using awk.i have tried with {print NR}
but its taking lot of time for a big file like 2GB file size.
so i want better efficiency...so can any body please help me with some other and better awk code?

Regards,
Pankaj
Did you try something similiar to this,

Code:
awk '{x++}END{ print x}' filename

As pointed out by vgersh,
wc -l < filename should do!
    #4  
Old 06-06-2007
panknil panknil is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 19 February 2013, 12:36 PM EST
Location: Kolkata
Posts: 90
Thanks: 2
Thanked 0 Times in 0 Posts
count lines of file

Dear vgersh99,

thanks for the reply but i have already tried with this command `wc -l`
its taking lot of time for 2GB size of file

Regards,
Pankaj
Sponsored Links
    #5  
Old 06-06-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 102 Times in 99 Posts
Panknil,
The only way to know how many lines a file has is to count
line by line, which is what "wc -l" does.
Anything else will use the same logic.
If it is taking too long, there is nothing else you can do.
Sponsored Links
    #6  
Old 06-06-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 16 Times in 16 Posts
Quote:
Originally Posted by panknil
Dear vgersh99,

thanks for the reply but i have already tried with this command `wc -l`
its taking lot of time for 2GB size of file

Regards,
Pankaj
how fast do you want it to be?
Sponsored Links
    #7  
Old 06-06-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 24 April 2014, 1:24 PM EDT
Location: Foxborough, MA
Posts: 7,541
Thanks: 137
Thanked 539 Times in 506 Posts
Quote:
Originally Posted by panknil
Dear vgersh99,

thanks for the reply but i have already tried with this command `wc -l`
its taking lot of time for 2GB size of file

Regards,
Pankaj
well.... as Shell_Life said... 'there ain't nothing you can do'.
a better question is 'WHY do you need to know the number of lines?'. Maybe you don't need to know the number of lines if you/we know the initial objective of all of this!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If file exists count lines holyearth Shell Programming and Scripting 3 01-01-2012 10:12 AM
count lines in file to variable gizmo16 Shell Programming and Scripting 4 10-21-2011 07:25 AM
WC -l does not count all the lines in a file? HELP Harleyrci Solaris 6 02-24-2011 04:34 AM
Trying to do a count on multiple lines in a file scottzx7rr Shell Programming and Scripting 7 02-17-2010 11:32 AM
Count of Number of Lines in a File sandeep_1105 UNIX for Dummies Questions & Answers 1 10-09-2009 01:10 PM



All times are GMT -4. The time now is 03:11 PM.