Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

count lines of file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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
Unix or Linux 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 Unix and Linux 06-06-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 2 July 2015, 5:01 AM EDT
Location: Foxborough, MA
Posts: 7,825
Thanks: 174
Thanked 653 Times in 610 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 Unix and Linux 06-06-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 9 April 2015, 4:19 AM EDT
Location: classification algos
Posts: 3,206
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 Unix and Linux 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 Unix and Linux 06-06-2007
Shell_Life's Unix or Linux Image
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 103 Times in 100 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 Unix and Linux 06-06-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 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 Unix and Linux 06-06-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 2 July 2015, 5:01 AM EDT
Location: Foxborough, MA
Posts: 7,825
Thanks: 174
Thanked 653 Times in 610 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 05:49 AM.