Reputation: 2635
I am trying to parse raw Service now ticket data into just the INC number and the short description. The script below does not throw an error; however, it is not giving me what I want.
#!/usr/bin/perl
use warnings;
use strict ;
#my $rawTicket = "$ARGV[0]" ;
my $line = "";
my $incnumb = "";
#open my $ticketPaste, '<', $rawTicket or die "Failed to open $rawTicket: $!\n";
#while( $line = <$ticketPaste> ) {
while( $line = <DATA> ) {
next if $line =~ /^$/ ;
next if $line =~ /\(empty\)/ ;
if ($line =~ /Select record for action.* Preview (INC\d+)/) {
$incnumb = $1;
}
next if $line =~ /Select record for action/ ;
print "$incnumb $line\n" ;
#sleep 1 ;
}
# close $ticketPaste or die "Failed to close $rawTicket: $!\n";
##Can't use string (" ") as a symbol ref while "strict refs" in use at ./ticket_watch.pl line 18, <$ticketPaste> line 7. <!-- did not like the 'my line' in line 7
__END__
(empty)
Ctas
?Select record for action ?Preview INC1008626119
(empty)
(empty)
(empty)
RE: RPM 117036-2 - New Service Request for CASPER
?Select record for action ?Preview INC1008625854
(empty)
(empty)
This is what it is giving me:
casper@CASPER ~$ ./rawTicketParse.pl
Ctas
RE: RPM 117036-2 - New Service Request for CASPER
However, this is What I am trying to get :
casper@CASPER ~$ ./rawTicketParse.pl
INC1008626119 Ctas
INC1008625854 RE: RPM 117036-2 - New Service Request for CASPER
Upvotes: 1
Views: 60
Reputation: 62154
You need to fix the regex and keep track of the previous line so that you can prepend the number:
use warnings;
use strict ;
my $prevline;
while (my $line = <DATA> ) {
next if $line =~ /^$/ ;
next if $line =~ /\(empty\)/ ;
if ($line =~ /Select record for action.*Preview (INC\d+)/) {
my $incnumb = $1;
print "$incnumb $prevline\n" ;
}
$prevline = $line;
}
Upvotes: 2