I am trying to parse the below text file (test.txt) using Perl script to get output format mentioned at bottom (bugid, description and username). Can you help me achieve it?
Data in test.txt
(1111) user1 <[email protected]> 112111: description1 - some dummy string
(6473) user2 <[email protected]> 112112: description2 - some test string
(1999) user3 <[email protected]> 129119: description3 - some tes3 string
(3975) user3 <[email protected]> 196234: description4 - some tes4 string
Here's the script that I am trying.
#!perl -w
#use strict;
no warnings;
my $ActivityLog = "test.txt";
my $ActListLog = "test3.txt";
open(FILE, "<$ActivityLog");
@prelist = <FILE>;
close (FILE);
foreach (@prelist)
{
if ($_ !~ /Bring over:/)
{
@postlist = split(".com> ", $_);
push (@result, $postlist[1]);
}
}
unlink $ActListLog;
open(LISTNAME,">$ActListLog")||die("cannot open the Input file");
print LISTNAME @result;
close LISTNAME;
Required Output:
112111: description1 user1
112112: description2 user2
129119: description3 user3
196234: description4 user3
CodePudding user response:
If the user and description is without spaces as in your example data:
#!/usr/bin/perl
use strict;
use warnings;
my $ActivityLog = 'test.txt';
open my $fh,'<', $ActivityLog or die "$ActivityLog: $!";
while(<$fh>) {
print "$2 $3 $1\n" if(/^\S (\S ) \S (\d :) (\S )/);
# user number description
}
close $fh;
CodePudding user response:
You can split on whitespace and just keep the 3 items that you need. Then print them out in the desired order:
use warnings;
use strict;
while (<DATA>) {
my (undef, $user, undef, $id, $desc) = split;
print "$id $desc $user\n";
}
__DATA__
(1111) user1 <[email protected]> 112111: description1 - some dummy string
(6473) user2 <[email protected]> 112112: description2 - some test string
(1999) user3 <[email protected]> 129119: description3 - some tes3 string
(3975) user3 <[email protected]> 196234: description4 - some tes4 string
Prints:
112111: description1 user1
112112: description2 user2
129119: description3 user3
196234: description4 user3
CodePudding user response:
Use this Perl one-liner:
perl -lne '( $username, $bugid, $description ) = m{ < (\S ) @ \S \s (\S :) \s (\S ) }xms; print join " ", $bugid, $description, $username;' test.txt > test3.txt
The Perl one-liner uses these command line flags:
-e
: Tells Perl to look for code in-line, instead of in a file.
-n
: Loop over the input one line at a time, assigning it to $_
by default.
-l
: Strip the input line separator ("\n"
on *NIX by default) before executing the code in-line, and append it when printing.
The regex uses this modifier:
/x
: Ignore whitespace and comments, for readability.
SEE ALSO:
perldoc perlrun
: how to execute the Perl interpreter: command line switches
perldoc perlre
: Perl regular expressions (regexes)
perldoc perlre
: Perl regular expressions (regexes): Quantifiers; Character Classes and other Special Escapes; Assertions; Capture groups
perldoc perlrequick
: Perl regular expressions quick start