0

我有一个这样的输入文件:

输入文件

我需要用 , 的值替换 .. 的值,@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);

} }

但是,我仍然没有得到文件中的更新值。

4

2 回答 2

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;

尝试整个代码。不需要单独的功能;这段代码完成了一切。

于 2013-09-06T08:01:57.060 回答
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;
}
于 2013-09-06T13:43:27.637 回答