Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How do I use grep in a script

by Flintlock (Novice)
on Dec 26, 2017 at 17:10 UTC ( [id://1206211]=perlquestion: print w/replies, xml ) Need Help??

Flintlock has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to extract a specific portion of a line found in a file to create a file name. I have this working for command line

grep Acct: Facs_Data.txt |cut -d":" -f2 |cut -d" " -f1

it returns the right half of the search i.e. Acct:1234 returns 1234. and I would then use this 1234 to create a file named 1234_2017.txt

So my question is: How can I achieve the same thing in Perl from opening a file that has more than one entry of Acct: ?

Replies are listed 'Best First'.
Re: How do I use grep in a script
by Corion (Patriarch) on Dec 26, 2017 at 17:18 UTC

    Converting the steps from a shell pipeline to Perl is fairly easy. The steps are:

    1. grep Acct: Facs_Data.txt - Read a file line by line and select the lines matching Acct:
    2. cut -d":" -f2 - Take the side to the right of : of the line
    3. cut -d" " -f1 - take the side to the left of the blank of the line

    If we convert each step to Perl, we get the following parts:

    1. my $filename = 'Facs_Data.txt'; open my $fh, '<', $filename or die "Couldn't open '$filename': $!"; my @lines = grep { /Acct:/ } <$fh>; # Read a file line by line and sel +ect the lines matching Acct:
    2. @lines = map { [ split $_, /:/ ]->[1] } @lines; # Take the side to the + right of : of the line
    3. @lines = map { [ split $_, /:/ ]->[0] } @lines; # take the side to the + left of the blank of the line

    But a more perlish approach would be to do all that in one go:

    my $filename = 'Facs_Data.txt'; open my $fh, '<', $filename or die "Couldn't open '$filename': $!"; my @lines = map { /:([^\s]+)/ ? $1 : () } # take the stuff between th +e : and the first blank grep { /Acct:/ } <$fh>; # Read a file line by line and sel +ect the lines matching Acct: # do whatever with the values in @lines print "$_\n" for @lines;

    Update:: Fixed missing = in step three, spotted by AnomalousMonk, thanks.

      map { /:([^\s]+)/ ? $1 : () } grep { /Acct:/ }
      is a long way to write
      map { /Acct:([^\s]+)/ }
        thank you - using your example, I have the following
        #! / usr/bin/perl -w use strict; use warnings; use POSIX 'strftime'; my $nm = strftime('%Y', localtime).".txt"; my $filename = 'Facs_Data.txt'; open my $fh, '<', $filename or die "Couldn't open '$filename': $!"; my @lines = map { /:([^\s]+)/ ? $1 : () } # take the stuff between th +e : and the first blank grep { /Acct:/ } <$fh>; # Read a file line by line and sel +ect the lines matching Acct: my $outfile = $_."_"."$nm" for @lines ; print $outfile; #open (OUTFILE, ">$outfile"); # do whatever with the values in @lines #print "$_\n" for @lines; close($filename);

        root@localhost:~/GoldenLFiles# perl facs.pl Use of uninitialized value $outfile in print at facs.pl line 17, <$fh> line 168801.

        If I comment the print $outfile to see the output I get the error above. What am I missing here? Thanks in advance
Re: How do I use grep in a script
by ForgotPasswordAgain (Priest) on Dec 26, 2017 at 17:19 UTC
      The OP wants to do it in Perl. I'm afraid it is somewhat counterproductive to suggest shelling out of Perl with backticks or qx//.
        Though there's value in learning how to do it in Perl, I respectfully disagree about the "counterproductive" part. The one-liner being done, just wrap backticks around it, use a split /\n/, and that's that (unless there are other constraints). I wouldn't be surprised if it were also faster, though that depends and might not matter.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1206211]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-18 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found