grep all records in a file and get a word count -perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep all records in a file and get a word count -perl
# 1  
Old 02-13-2008
grep all records in a file and get a word count -perl

Hi,

I have a file .. file.txt .. i need to get a total record count in the files into a $variable.. im using perl script

thanks
# 2  
Old 02-13-2008
wc -l filename gives a record count for a file
in perl
Code:
$variable = `wc -l filename`

wc -c counts the words in a file. It is hard to see which one you want.
# 3  
Old 02-13-2008
Hey thanks for the reply.. but
wc -l filename ... gives (say count is 200)

output:
200 filename

-- the count includes filename too.. and i want to avoid it..

Thanks
# 4  
Old 02-13-2008
Not sure how efficient this is

Code:
open (FH, 'filename');
(1) while (<FH>);
my $wc = $. || 0;
print $wc;

$. is the input file line/record number. The way I used it it will give the last record number of the last file opened, if you need the record number for multiple files push $. into an array instead of assigning to a scalar.
# 5  
Old 02-13-2008
Quote:
Originally Posted by meghana
Hey thanks for the reply.. but
wc -l filename ... gives (say count is 200)

output:
200 filename

-- the count includes filename too.. and i want to avoid it..

Thanks
a simple split on the variable and getting the first element will do it. look up
perldoc -f split..
please read through the Perl docs
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Use GREP to count number of records and place it in a variable

I am trying to count the number of records from different files using grep, and then place the result in a separate variable for each file, so at the end of my shell script, I can sum all the variables and check if the number of records are equal to what I was expecting. It is weird butwc -ldoes... (2 Replies)
Discussion started by: dhruuv369
2 Replies

3. Shell Programming and Scripting

perl count lines with certain word int

Hi Guys I have a text file that contains the message like this /var/log/messages.all-20120401: Mar 26 12:12:23 brent kernel: NVRM: Xid (0003:00): 43, 0005 00009097 00000000 00000000 00001b0c 1000f010 /var/log/messages.all-20120401: Mar 27 20:42:40 brent kernel: NVRM: Xid (0003:00): 43,... (4 Replies)
Discussion started by: ab52
4 Replies

4. UNIX for Dummies Questions & Answers

word count with grep

Hi, It is very interesting to learn the unix, i just struck with a doubt like i have below content in my file xyz xyz xyz xyz i just want know the word count by using grep -wc 'xyz' <filename>, but it is giving 3 instead of 4.So i understood that it is showing matched line numbers count... (2 Replies)
Discussion started by: vmachava
2 Replies

5. Shell Programming and Scripting

Perl code- word count problem

Hi, I am having .csv files contains some row - Info: Value of field name 'SecurityExchange' is not supported ","Original Order Tuple Please see the below perl code carefully- /Info: (+),Original (\w+) Tuple/ and do { ($category, $type) = ($1, $2); if($type eq 'Execution')... (1 Reply)
Discussion started by: pspriyanka
1 Replies

6. UNIX for Dummies Questions & Answers

grep word count

How do I do a grep wc that counts occurences of two or more different strings? If I have string1 two times and string2 three times, how do I use wc to get the number 5? (1 Reply)
Discussion started by: locoroco
1 Replies

7. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

8. UNIX for Dummies Questions & Answers

if clause with grep and word count

I've got following script that I thought would only email me when the if clause finds the grep. But it emails me anyway (with an empty file) even if the grep doesn't return anything. What should the line be? if then grep -l 'unique constraint' $LOGDIR/archive_active* >... (3 Replies)
Discussion started by: Meert
3 Replies

9. UNIX for Dummies Questions & Answers

Count records in a zip file

Hello, I searched the forums on the keywords in the title I used above, but I did not find the answer: Is it possible to count records in a .zip file on an AIX machine if i don't have pkunzip installed? From all the research I'm reading in google and the reading of pkunzip in Unix.com,... (3 Replies)
Discussion started by: tekster757
3 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question