Perl - line count of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - line count of a file
# 1  
Old 11-03-2014
Perl - line count of a file

Hi,

I am quite new to perl scripting.

I have a dat file (datFile) from which I am pulling only first column and saving the output to a new file (f). From that file (f) I am removing blank lines and saving it to new file (datFile1). I am able to get the count of $f file in variable $cnt. But I am unable to get the count of $datFile1 in $cnt1.

Please find below my code.

Code:
my $cnt;
my $cnt1;
my $f = $datFile."_Column.dat";
my $datFile1= $datFile."_null.dat";
open my $fh_in,  "<", "$datFile"  or die "Could not open $datFile: $!\n";
open my $fh_out, ">", "$f" or die "Could not create $f: $!\n";
while ( my $line = <$fh_in> ) {
    my $first_column = (split /\t/, $line)[0];
    print $fh_out "$first_column\n";
}
close $fh_in;
close $fh_out;
open my $fh_in1,  "<", "$f"  or die "Could not open $f: $!\n";
open my $fh_out1, ">", "$datFile1" or die "Could not create $datFile1: $!\n";
while ( <$fh_in1> ) {
    if (/\S/) {
        print $fh_out1 $_;
    }
}
open(FH,$datFile) or die "Damn. $!";
$cnt++ while <FH>;
close FH;
print "$cnt\n";
open(FHN,$datFile1) or die "Damn. $!";
$cnt1++ while <FHN>;
close FHN;
print "$cnt1\n";
$logger->info($ID." ".$cnt." Check count cnt");
$logger->info($ID." ".$cnt1." Check count cnt1");

Regards
Neethu
# 2  
Old 11-03-2014
Is it possible that $datFile is holding/representing a file name without extension?
# 3  
Old 11-03-2014
File Name of a sample $datfile - Sample_20141014_100.dat
File Name of sample $f - Sample_20141014_100.dat_Column.dat
File Name of sample $datFile1 - Sample_20141014_100.dat_null.dat

When I replace $datFile1 in the below code to $datFile then I am able to get the count for $datFile in $cnt1.

Code:
open(FHN,$datFile1) or die "Damn. $!";
$cnt1++ while <FHN>;
close FHN;
print "$cnt1\n";

Regards
Neethu
# 4  
Old 11-03-2014
Please try
Code:
open(FHN,"$datFile1") or die "Damn. $!";

# 5  
Old 11-04-2014
I changed to
Code:
open(FHN,"$datFile1") or die "Damn. $!";

but it didnt work.

---------- Post updated at 09:54 AM ---------- Previous update was at 08:58 AM ----------

Thank you for your time.

I am able to solve it.

Regards
Neethu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

2. Shell Programming and Scripting

To take line count from a file

Hi guys, I am having a file which where i need to take line count based on searching a particular string from a list say list_file.txt which occurs in 2nd column of my main file and to take the line count which doesnot exist in list file say list_file.txt for eg: my main file looks like this... (4 Replies)
Discussion started by: rohit_shinez
4 Replies

3. Shell Programming and Scripting

FASTEN count line of dat file and compare with the CTRL file

Hi All, I thinking on how to accelerate the speed on calculate the dat file against the number of records CTRL file. There are about 300 to 400 folder directories that contains both DAT and CTL files. DAT contain all the flat files records CTL is the reference check file for the... (3 Replies)
Discussion started by: ckwan
3 Replies

4. UNIX for Advanced & Expert Users

How to take file line count in AIX

HI All, I am trying to take file line count in UNIX and AIX Unix command to take file line count-- working fine count=`wc -l /apps/hgford/sorted/E.testing.DLY|cut -f1 -d " "` 1355Same command when run in aix --having issue count=`wc -l /apps/hgford/sorted/E.testing.DLY|cut -f1 -d " "`... (5 Replies)
Discussion started by: Perlbaby
5 Replies

5. Shell Programming and Scripting

Perl - Sort and Count foreach line

Can any one please help I am trying to sort each item in every line and count the common (non case sensitive) and at the end printing all the unique alphabetically. Here is what i did ... I can print all the lines but struck to sort each line some were: I am getting the output as: I... (5 Replies)
Discussion started by: sureshcisco
5 Replies

6. Shell Programming and Scripting

Perl line count if it matches a pattern

#!/usr/bin/perl use Shell; open THEFILE, "C:\galileo_integration.txt" || die "Couldnt open the file!"; @wholeThing = <THEFILE>; close THEFILE; foreach $line (@wholeThing){ if ($line =~ m/\\0$/){ @nextThing = $line; if ($line =~ s/\\0/\\LATEST/g){ @otherThing =... (2 Replies)
Discussion started by: nmattam
2 Replies

7. Shell Programming and Scripting

File Line Count

Hi, Came across a weird problem today. I was just trying to write this small script which would read the number of lines in a file. Depending on the count some further processing would be done. I used wc -l in order to get that done. But since it depends on the number of new line characters, if... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

Get the line count from 2nd line of the file ?

Hi, I want to get the line count of the file from the 2nd line of the file ? The first line is header so want to skip that. Thanks. (8 Replies)
Discussion started by: smc3
8 Replies

9. Shell Programming and Scripting

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 (4 Replies)
Discussion started by: meghana
4 Replies

10. Shell Programming and Scripting

hw can i count the number of character in a file by perl

i want to count the number of character contained in afile using perl cript help me out (1 Reply)
Discussion started by: trupti_rinku
1 Replies
Login or Register to Ask a Question