Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-04-2007
Registered User
 
Join Date: Sep 2006
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
total number of lines in a file

Hi ,

How about find the total number of lines in a file ?
How can i do that with the "grep" command ?
Sponsored Links
    #2  
Old 10-04-2007
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by Raynon View Post
Hi ,

How about find the total number of lines in a file ?
How can i do that with the "grep" command ?
Please dont hijack another thread. I am creating a new thread.

You can use wc to find the number of lines.
Code:
wc -l < file.txt

Using grep you can use


Code:
grep -c "." file.txt

Sponsored Links
    #3  
Old 10-04-2007
radoulov's Avatar
--
 
Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 5,468
Thanks: 139
Thanked 538 Times in 506 Posts
Quote:
Originally Posted by vino View Post
Please dont hijack another thread. I am creating a new thread.

You can use wc to find the number of lines.
Code:
wc -l < file.txt

Using grep you can use


Code:
grep -c "." file.txt


Code:
zsh 4.3.4% cat file
a

b
zsh 4.3.4% wc -l file
3 file
zsh 4.3.4% grep -c . file
2

may be something like:


Code:
zsh 4.3.4% grep -c ^ file
3

and it's not bulletproof either ...

Last edited by radoulov; 10-04-2007 at 06:02 AM.. Reason: spelling ...
    #4  
Old 10-04-2007
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by radoulov View Post
Code:
zsh 4.3.4% cat file
a

b
zsh 4.3.4% wc -l file
3 file
zsh 4.3.4% grep -c . file
2

may be something like:


Code:
zsh 4.3.4% grep -c ^ file
3

and it's not bulletproof either ...
Hmm.. my input file had empty lines or so I thought. It had the ^M characters which resulted into the same result for both wc and grep.
Sponsored Links
    #5  
Old 10-04-2007
Registered User
 
Join Date: Sep 2006
Posts: 2,651
Thanks: 0
Thanked 15 Times in 15 Posts
Quote:
Originally Posted by Raynon View Post
Hi ,

How about find the total number of lines in a file ?
How can i do that with the "grep" command ?
i find it strange that after joining the forum for so long, you still don't know how to do that?
Sponsored Links
    #6  
Old 10-04-2007
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by ghostdog74 View Post
i find it strange that after joining the forum for so long, you still don't know how to do that?
I suspect homework questions.
Sponsored Links
    #7  
Old 10-04-2007
Registered User
 
Join Date: Sep 2006
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
Hi ghostdog,

I think i know how to do it. See below!


Code:
wc -l myfile|awk '{print($1)}'`

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
Write the total number of rows in multiple files into another file malaya kumar UNIX for Dummies Questions & Answers 4 03-30-2012 04:46 AM
Select lines in which column have value greater than some percent of total file lines vaibhavkorde Shell Programming and Scripting 6 04-21-2011 04:42 AM
perl script on how to count the total number of lines of all the files under a directory adityam Shell Programming and Scripting 5 07-07-2010 04:36 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 10:19 AM
total number of lines mahabunta Shell Programming and Scripting 4 01-31-2007 04:11 AM



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