in reply to How do I use grep in a script
Converting the steps from a shell pipeline to Perl is fairly easy. The steps are:
If we convert each step to Perl, we get the following parts:
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:
@lines = map { [ split $_, /:/ ]->[1] } @lines; # Take the side to the + right of : of the line
@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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I use grep in a script
by ikegami (Patriarch) on Dec 26, 2017 at 17:35 UTC | |
by Flintlock (Novice) on Dec 26, 2017 at 20:20 UTC | |
by Corion (Patriarch) on Dec 26, 2017 at 20:27 UTC | |
by Flintlock (Novice) on Dec 26, 2017 at 21:07 UTC | |
by poj (Abbot) on Dec 26, 2017 at 21:53 UTC | |
|