0

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
1
  • 1
    #use strict; and no warnings; means you are telling the interpreter not to help you at all. Commented Oct 8, 2021 at 13:39

3 Answers 3

3

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;

If there can be spaces in the user and 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(/^\(\d+\)\s+(.*)\s+<\S+?>\s+(\d+:)\s+(.*?)\s+-/);
    #                                 user            number   description
}
close $fh;
Sign up to request clarification or add additional context in comments.

2 Comments

Problem with this approach is, I have multi-spaced description and multi-spaced comments , after task-id. I am losing part of description.
Reverted the data back to original
2

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

1 Comment

Thanks everyone for your valuable inputs. I will try these and share the outcome.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.