我有一个像这样的输入文件:
我需要将@pSBSB_ID="*"
的值@rectype=@pRECTYPE="SBSB"
替换为@pMEME_SSN="034184233"
,值为@pRECTYPE="SMSR"
,..并且必须删除@rectype='@pRECTYPE="SMSR",
'的行
实施例: 因此,在进行更改后,文件应如下所示:
....@pRECTYPE="SBSB", @pGWID="17199269", @pINPUT_METHOD="E", @pGS08="005010X220A1", @pSBSB_FAM_UPDATE_CD="UP", @pSBSB_ID="034184233".....
....@pRECTYPE="SBEL", @pSBEL_EFF_DT="01/01/2013", @pSBEL_UPDATE_CD="TM", @pCSPD_CAT="M", @pCSPI_ID="MHMO1003"
.
.
.
更新
我尝试了下面提到的代码: 输入文件扩展名:mms,有多个文件要处理。
my $save_for_later;
my $record;
my @KwdFiles;
my $r;
my $FilePath = $ARGV[0];
chdir($FilePath);
@KwdFiles = <*>;
foreach $File(@KwdFiles)
{
unless(substr($File,length($File)-4,length($File)) eq '.mms')
{
next;
}
unless(open(INFILE, "$File"))
{
print "Unable to open file: $File";
exit(0);
}
print "Successfully opened the file: \"$File\" for processing\n\n";
while ( my $record = <INFILE> ) {
my %r = $record =~ /\@(\w+) = '(.*?)'/xg;
if ($r{rectype} eq "SMSR") {
$save_for_later = $r{pMEME_SSN};
next;
}
elsif ($r{rectype} eq "SBSB" and $r{pSBSB_ID} eq "*") {
$record =~ s|(\@pSBSB_ID = )'.*?'|$1'$save_for_later'|x;
}
close(INFILE);
} }
但是,我仍然没有在文件中获取更新的值。
答案 0 :(得分:0)
#!/usr/bin/perl
open IN, "< in.txt";
open OUT, "> out.txt";
my $CUR_RECID = 1^1;
while (<IN>) {
if ($CUR_RECID) {
s/recname='.+?'/recname='$CUR_RECID'/ if /rectype='DEF'/;
$CUR_RECID = 1^1;
print OUT;
}
$CUR_RECID = $1 if /rectype='ABC'.+?rec_id='(.+?)'/;
}
close OUT;
close IN;
尝试整个代码。不需要单独的功能;这段代码完成了所有工作。
答案 1 :(得分:0)
从终端运行此脚本,并将要修改的文件作为参数:
use strict;
use warnings;
$^I = '.bak'; #modify original file and create a backup of the old ones with .bak appended to the name
my $replacement;
while (<>) {
$replacement = $1 if m/(?<=\@pMEME_SSN=)("\d+")/; #assume replacement will be on the first line of every file.
next if m/^\s*\@pRECTYPE="SMSR"/;
s/(?<=\@pSBSB_ID=)("\*")/$replacement/g;
print;
}