quickest way to get the total number of lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting quickest way to get the total number of lines in a file
# 1  
Old 09-30-2012
quickest way to get the total number of lines in a file

i have a file that's about 2GB, i have to get the total number of lines in this file every 10 minutes.

the interval is not an issue. i just need the proper, most efficient way to do this.

any ideas?

i got the following from another thread on this site, but:

Code:
awk 'int(100*rand())%5<1' file

but this randomly pulls out 20% of lines in a file. i'm thinking this code can be slightly modified to get what i want?
# 2  
Old 09-30-2012
Code:
wc -l filename

# 3  
Old 09-30-2012
Quote:
Originally Posted by rdrtx1
Code:
wc -l filename

i'd rather not use "wc -l". it is not efficient at all to be used on a file size of 2GB. i'm hoping there's a better way to get the total line number of a file that big.
# 4  
Old 09-30-2012
Code:
awk 'END {print NR}' filename

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 09-30-2012
Quote:
Originally Posted by SkySmart
i'd rather not use "wc -l". it is not efficient at all to be used on a file size of 2GB. i'm hoping there's a better way to get the total line number of a file that big.
You do realize, don't you, that in order to count the number of <newline> characters in a file you have to read the entire file?

If lines are being added to the file constantly, but existing data in the file isn't changing, you could save the line count and file size at the time of your last check. Then when the next size check occurs, you could just read the new data and count the added lines and add the results to your previous count. If you're really concerned about efficiency, you probably want to write this in C or C++ rather than a shell script.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-30-2012
Sky Smart - can you cite a verified reference as to why reading a file from record 1 to EOF is NOT the most efficient for carriage control files. rdrtx1's sample code does that and so does wc -l

I'll answer:
No such valid reference exists. You have to count the number of \n characters to get a line count. The only other possibility is for a fixed length record file. In that case you call stat, ls, or some code you have to get the number of bytes (reading file metadata: struct stat st_size) and then do integer division: bytes/recsz.

One other ' reliable' way is to call x=ftell() on the end of a file when you know the file has finished being written and divide x/recsz - again for fixed record length files. This is an even less efficient way to do stat.


NOTHING else exists. In other words: how can you know how many \n characters exist in the file?
This User Gave Thanks to jim mcnamara For This Post:
# 7  
Old 10-01-2012
Quote:
Originally Posted by jim mcnamara
Sky Smart - can you cite a verified reference as to why reading a file from record 1 to EOF is NOT the most efficient for carriage control files. rdrtx1's sample code does that and so does wc -l

I'll answer:
No such valid reference exists. You have to count the number of \n characters to get a line count. The only other possibility is for a fixed length record file. In that case you call stat, ls, or some code you have to get the number of bytes (reading file metadata: struct stat st_size) and then do integer division: bytes/recsz.

One other ' reliable' way is to call x=ftell() on the end of a file when you know the file has finished being written and divide x/recsz - again for fixed record length files. This is an even less efficient way to do stat.


NOTHING else exists. In other words: how can you know how many \n characters exist in the file?
when you run a "wc -l" on a file that big, it takes a while to get the total line count. i understand that the entire file must be read in order to get the total lines.

i'm also aware that in UNIX, there are more than one ways to get something done. on some linux systems a "grep -P" will get you want you want a lot faster than any other utility can. on others, the -P option is not available.

overall, i'm more concerned about speed and what the quickest way is to get total line count on a file that big.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to find count total number of pattern in a file …?

How to find count total number of pattern in a file … File contains : a.txt ------------- aaa bbb nnn ccc aaa bbb aaa ddd aaa aaa aaa aaa grep -c aaa a.txt Op: 4 ( But my requirement is should count the total no of patterns as 7 ) (4 Replies)
Discussion started by: Jitten
4 Replies

2. UNIX for Dummies Questions & Answers

Write the total number of rows in multiple files into another file

Hello Friends, I know you all are busy and inteligent too... I am stuck with one small issue if you can help me then it will be really great. My problem is I am having some files i.e. Input.txt1 Input.txt2 Input.txt3 Now my task is I need to check the total number of rows in... (4 Replies)
Discussion started by: malaya kumar
4 Replies

3. Shell Programming and Scripting

Help with sum total number of record and total number of record problem asking

Input file SFSQW 5192.56 HNRNPK 611.486 QEQW 1202.15 ASDR 568.627 QWET 6382.11 SFSQW 4386.3 HNRNPK 100 SFSQW 500 Desired output file SFSQW 10078.86 3 QWET 6382.11 1 QEQW 1202.15 1 HNRNPK 711.49 2 ASDR 568.63 1 The way I tried: (2 Replies)
Discussion started by: patrick87
2 Replies

4. Shell Programming and Scripting

Select lines in which column have value greater than some percent of total file lines

i have a file in following format 1 32 3 4 6 4 4 45 1 45 4 61 54 66 4 5 65 51 56 65 1 12 32 85 now here the total number of lines are 8(they vary each time) Now i want to select only those lines in which the values... (6 Replies)
Discussion started by: vaibhavkorde
6 Replies

5. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

6. Shell Programming and Scripting

Removing lines from large files.. quickest method?

Hi I have some files that contain be anything up to 100k lines - eg. file100k I have another file called file5k and I need to produce filec which will contain everything in file100k minus what matches in file 5k.. ie. File100k contains 1FP 2FP 3FP File5k contains 2FP I would... (2 Replies)
Discussion started by: frustrated1
2 Replies

7. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

8. Shell Programming and Scripting

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 ? (9 Replies)
Discussion started by: Raynon
9 Replies

9. Shell Programming and Scripting

total number of lines

Hi have following file |abcd 2|abcd |sdfh |sdfj I want to find total number of files haivng nothing in feild 1 using awk will command awk -F "|" '( $1=="") {print NR}' test_awk will work??? (4 Replies)
Discussion started by: mahabunta
4 Replies

10. Shell Programming and Scripting

Total of lines w/out header and footer incude for a file

I am trying to get a total number of tapes w/out headers or footers in a ERV file and append it to the file. For some reason I cannot get it to work. Any ideas? #!/bin/sh dat=`date +"%b%d_%Y"` + date +%b%d_%Y dat=Nov16_2006 tapemgr="/export/home/legato/tapemgr/rpts"... (1 Reply)
Discussion started by: gzs553
1 Replies
Login or Register to Ask a Question