Perl programming issue

Dears,

I want to print filename and count of each file in perl but failing to implement.

`find $srcFolder -maxdepth 1 -type f -name "*$workDate*$fileExt" -exec sh -c '[ -f "$0" ] &&  printf "$workDate|%s|%s\n" "$(wc -l<"$0")" *$workDate*$fileExt' {} \ >> /Sadique/filelog.out \\; 2> /dev/null`;

Output :

2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|
2019-02-18|30501 30501wc -l<FileAgg.pl)|

Expected Output :

|2019-02-19|402|./Trigger_NLB-B2C_2019-02-18_02-37-09_000000_1.txt
|2019-02-19|58661|./Trigger_NLB-B2C_2019-02-18_12-59-27_000000_1.txt
|2019-02-19|16680|./Trigger_NLB-B2B_2019-02-18_13-07-42_000000_1.txt
|2019-02-19|2543|./Trigger_NLB-B2C_2019-02-18_06-59-10_000000_1.txt
|2019-02-19|3161|./Trigger_NLB-B2C_2019-02-18_05-59-09_000000_1.txt
|2019-02-19|24209|./Trigger_NLB-B2B_2019-02-18_10-07-41_000000_1.txt

Why are you using PERL?

Using Perl only for determining line counts just does not seem to be an efficient way of doing things.
You will have more points of failure in your script. Also, more code means more to troubleshoot, more to debug.
"wc -l" does the job, and does it very well.

Nevertheless, if you want to know how it can be done, then see below.
You may want to time each approach and pick the one that suits your code if you have large files and/or file count.

$ 
$ cat -n FileAgg.pl
     1    #!/usr/bin/perl
     2    use strict;
     3    my ($workDate, $file) = @ARGV;
     4    my $lineCount;
     5    open(FH, "<", $file) or die "Can't open $file: $!";
     6    while (<FH>) {
     7        $lineCount++;
     8    }
     9    close(FH) or die "Can't close $file: $!";
    10    printf("%s|%s|%s\n", $workDate, $lineCount, $file);
    11    
$ 
$ cat -n report_filecounts.sh 
     1    #!/bin/bash
     2    # Create dummy text files that contain today's date in their names
     3    rm *.txt
     4    for n in $(seq 1 10); do
     5        echo "This is line $n"
     6    done > "file1_$(date '+%Y-%m-%d').txt"
     7    
     8    for n in $(seq 1 20); do
     9        echo "This is line $n"
    10    done > "file2_$(date '+%Y-%m-%d').txt"
    11    
    12    # Set and export a few shell variables.
    13    # The Perl program is an executable and is passed to this script.
    14    export srcFolder=.
    15    export workDate=$(date '+%Y-%m-%d')
    16    export fileExt=txt
    17    export perlProgram=$1
    18    
    19    echo "==== Without Perl ===="
    20    find $srcFolder -maxdepth 1 -type f \
    21         -name "*$workDate*$fileExt" \
    22         -exec sh -c 'printf "$workDate|%s|%s\n" $(wc -l {})'  \;
    23    
    24    echo
    25    echo "==== With Perl Program ===="
    26    find $srcFolder -maxdepth 1 -type f \
    27         -name "*$workDate*$fileExt" \
    28         -exec sh -c './$perlProgram $workDate {}' \;
    29    
    30    # All the work in FileAgg.pl (opening/closing files, setting input
    31    # parameter array, setting line counts etc.) is done for you by the
    32    # perl command-line interpreter with command switches -l, -n, -e
    33    # so you could use that alternatively
    34    echo
    35    echo "==== With Perl one-liner ===="
    36    find $srcFolder -maxdepth 1 -type f \
    37         -name "*$workDate*$fileExt" \
    38         -exec perl -lne '}{ printf("%s|%s|%s\n", $ENV{workDate}, $., $ARGV)' {} \;
    39    
    40    
$ 
$ . report_filecounts.sh FileAgg.pl
==== Without Perl ====
2019-02-21|20|./file2_2019-02-21.txt
2019-02-21|10|./file1_2019-02-21.txt

==== With Perl Program ====
2019-02-21|20|./file2_2019-02-21.txt
2019-02-21|10|./file1_2019-02-21.txt

==== With Perl one-liner ====
2019-02-21|20|./file2_2019-02-21.txt
2019-02-21|10|./file1_2019-02-21.txt
$ 
$ 
1 Like