Perl programming issue


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl programming issue
# 1  
Perl programming issue

Dears,

I want to print filename and count of each file in perl but failing to implement.
Code:
`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 :
Code:
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 :
Code:
|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


Last edited by Neo; 02-19-2019 at 11:28 AM..
# 2  
Why are you using PERL?
# 3  
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.

Code:
$ 
$ 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
$ 
$


Last edited by durden_tyler; 02-21-2019 at 06:09 AM..
This User Gave Thanks to durden_tyler For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #321
Difficulty: Medium
DHCP stands for Dynamic Host Configuration Port.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

AIX runtime programming issue

I hope my title is accurate enough. I have a product that we port to various UNIX platforms. It is known to run on AIX but using the IBM compiler from years ago. Recently we got a different used AIX P5 platform running AIX 5.3 and we setup the GCC compiler (4.4.5 I think). C and C++ source code.... (5 Replies)
Discussion started by: Pug
5 Replies

2. Shell Programming and Scripting

Linux shell programming performance issue

Hi All, can any one help me on this please. Replace sting in FILE1.txt with FILE2.txt. FILE1.txt record must have at least one state is repeated once.But need to replace only from second occurrence in record in FILE1.txt Condition: order of searching the records in FILE2.txt is impartent.... (8 Replies)
Discussion started by: ureddy
8 Replies

3. Shell Programming and Scripting

Need help with programming issue

I need a UNIX shellscript command that will remove the 'Paragraph' mark from a simple text file. The Paragraph mark I'm referring to is the special character you can see in a WORD documant (similar to a backwards 'P') when you select the Show Paragraph Marks icon. If someone can give me the... (6 Replies)
Discussion started by: varefump
6 Replies

4. Shell Programming and Scripting

Perl programming help

I am trying to make a simple perl program that reads 20 characters upstream from the codon ATG in a given sequence. The following is what I have. I just dont know how to make the program read 20 characters upstream from the ATG codon. print "\nThis program will read 20 characters upstream... (1 Reply)
Discussion started by: patiencenpray
1 Replies

5. Shell Programming and Scripting

perl programming

how to link the linux files in perl on the local webpage ???? suppose we have some results and want to get them published on the local webpage of our internal site. how this can be done using HTML and perl together , so that the results are published directly on the webpage. thanks kullu (0 Replies)
Discussion started by: kullu
0 Replies

6. Programming

Help me with perl programming

Hi, i am very beginer to perl, I am reading one xml file and i am creating hash table for that file. i written code like this #!/usr/bin/perl use warnings; use strict; use XML::LibXML::Reader; #Reading XML with a pull parser my $file; open( $file, 'formal.xml'); my $reader =... (8 Replies)
Discussion started by: veerubiji
8 Replies

7. Programming

Programming help - Perl !

I am having a text file with Vivek 50 Ram 34 Hulk 45 Vivek 23 Ram 23 Vivek 55 Now I need a perl script to display the fields of 1st column & the 2nd column with summation (& avoid the duplicates). Vivek 128 Ram 57 hulk 45 Plz help me... (1 Reply)
Discussion started by: gameboy87
1 Replies

8. Shell Programming and Scripting

Perl programming error

Hi, everyone!! i am new to perl programming.. plz help me. #!C:/perl/bin use warnings; use strict; use Text::CSV_XS; my @rows = ""; my $row; my $count; my $fh; my @fields = ""; my $csv = Text::CSV_XS->new ({binary =>1}) or die "cannot use CSV:" .Text::CSV->error_diag (); open... (3 Replies)
Discussion started by: kvth
3 Replies

9. Shell Programming and Scripting

Sed issue in K Shell programming

I am doing the following script in k shell sed -i 's/FILENAME/$i/g' TEST/test$j.ctl > TEST/control$j.ctl In the file it replaces $i for all FILENAME, it doesnot replace with the value of i. I put single quotes like below sed -i 's/FILENAME/'$i'/g' TEST/test$j.ctl > TEST/control$j.ctl I... (9 Replies)
Discussion started by: toshidas2000
9 Replies

10. UNIX for Dummies Questions & Answers

PERL - DB programming

Hi friends, What are the possible ways to connect to DB2 database from Perl (on unix). I need to connect to DB2 and get records for further processing. Can you please suggest the best possible way. I heard about DBI/DBD, if you have some sample scripts please post them too. Thanks in advance. (3 Replies)
Discussion started by: satguyz
3 Replies

Featured Tech Videos