perl - 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
解决方案
您需要修复正则表达式并跟踪上一行,以便您可以预先添加数字:
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;
}
推荐阅读
- javascript - 仅切换包含相同文本的 div
- mysql - 在其他选择中使用列值
- node.js - 当我触摸屏幕底部时自动滚动 ScrollView
- angularjs - 如何为 ionic v1 构建配置环境变量?
- sql-server - 检查匹配
- java - Calendar.getTime 返回不正确的日期
- mysql - 如何从此联接中获取“NULL”数据?
- firebase - Firebase 身份验证仅将 Facebook 和电子邮件身份验证转换为 Google 身份验证,反之则不然
- c# - 特定记录器名称的 Nlog 规则不起作用
- haskell - 从 Signal Int 到 Haskell 中的 Int