count lines of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count lines of file
# 1  
Old 06-06-2007
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
# 2  
Old 06-06-2007
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?
# 3  
Old 06-06-2007
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
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
# 5  
Old 06-06-2007
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.
# 6  
Old 06-06-2007
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?
# 7  
Old 06-06-2007
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If file exists count lines

Hello, Currently I have: FILE=/home/file.txt if ; then echo "File $FILE exists" else echo "File $FILE does not exist" fi exit I would like to make it such that if the file *does* exist, it performs a wc -l count of the file and then if the count is greater than 3 performs... (3 Replies)
Discussion started by: holyearth
3 Replies

2. Shell Programming and Scripting

count lines in file to variable

I have a text file in which you need to identify the number of lines that looks like this: awk '{x + +} END {print x}' filename The problem is that I do not know how this data to any variable in which then need to continue to work in a cycle for .. do not know someone help? Sorry for my... (4 Replies)
Discussion started by: gizmo16
4 Replies

3. Shell Programming and Scripting

Help with script to read lines from file and count values

Hi, I need some help with a script I'm trying to write. I have a log file containing references to a number of different webservices. I wish to write a script that will list the webservices with a count as to how many times they appear in the log. An example of the log file content: ... (2 Replies)
Discussion started by: gman2010
2 Replies

4. Solaris

WC -l does not count all the lines in a file? HELP

I have a file that I need to merge with another like file. Normally I remove the trailer reocrd and merge the file and update the trailer record of the second file. I did a WC -l on the first file before I removed the trailer record, and again afterwards. The count came back the same. I opened the... (6 Replies)
Discussion started by: Harleyrci
6 Replies

5. Shell Programming and Scripting

Trying to do a count on multiple lines in a file

Guys I am having a problem with being able to do a count of entries in a file. What I am trying to get a count of the total number of members that are listed in the files. So I need to pull the number of the lines after members. I tried using sed but it only seems to count the first... (7 Replies)
Discussion started by: scottzx7rr
7 Replies

6. Shell Programming and Scripting

Count the number of lines in a file with one condition

Hi Everyone, 1.txt Mon 08 Feb 2010 12:30:44 AM MYT;1265560244;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:46 AM MYT;1265560246;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:48 AM MYT;1265560248;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:50 AM... (1 Reply)
Discussion started by: jimmy_y
1 Replies

7. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

8. Shell Programming and Scripting

Count lines between two patterns inside a file

Hi, Im doing a script to find the number of lines included inside a file newly. These lines are in between #ifdef FLAG1 and #else or #endif or #else and #endif. I tried like this, awk '/#ifdef Flag1/,/#e/{print}' aa.c | wc -l awk '/#ifndef Flag1/,/#endif/{print}' aa.c | awk... (6 Replies)
Discussion started by: priyadarshini
6 Replies

9. Programming

to count the number of commented lines in a file

can someone guide me how to have a C pgm to count the number of commented lines? (3 Replies)
Discussion started by: naan
3 Replies

10. Shell Programming and Scripting

Count number of lines in Binary file

Hi Friends Please help me out to count number of lines in binary file. It gives some wrong(less) using wc -l. Is there any other way to count lines of binary file. Thanks. (3 Replies)
Discussion started by: vanand420
3 Replies
Login or Register to Ask a Question