首页 > 解决方案 > Parse service now ticket data in Perl INC and short description

问题描述

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

标签: perl

解决方案


您需要修复正则表达式并跟踪上一行,以便您可以预先添加数字:

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;
}

推荐阅读