未终止的`s'命令 [英] Unterminated `s' command

查看:64
本文介绍了未终止的`s'命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 perl 脚本,该脚本将获取客户服务信息并更改服务 ID.我现在拥有的代码可以使用 2 列 csv 文件作为参考,并交换数字,但是当我尝试使用\n"或\|"时就像我需要的那样,它会给我`ol:

I am working on a perl script that finds will take customer service information and change the service ids. The code i have now can take a 2 column csv file for reference, and swap the numbers, but when i try to use "\n" or "\|" like i need to, it will give me the `ol:

"sed: -e 表达式 #1, char 29: 未终止的 `s' 命令"

"sed: -e expression #1, char 29: unterminated `s' command"

这是我的原始代码,仅用于更改引号内的数字:

Here's my original code that works for changing JUST the number within the quotes:

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV; #load Text::CSV module for parsing CSV
use LWP::Simple; #load LWP module for doing HTTP get

my $file = 'sid_swap.csv'; #CSV file to parse and load
my $csv = Text::CSV->new(); #create a new Text::CSV object

open (CSV, "<", $file) or die $!; #open CSV file for parsing

while (<CSV>) {
    if ($csv->parse($_)) {

        my @columns = $csv->fields(); #parse csv files and load into an array for each row
        my $newSID = $columns[0];
        my $oldSID = $columns[1];
        system("sed -i 's/\<row SERVICE_MENU_ID=\"$oldSID\"/\<row SERVICE_MENU_ID=\"$newSID\"/g' customer_data.txt");

    } else {
        my $err = $csv->error_input;
        print "Failed to parse line: $err";
    }
}
close CSV;

这是引发错误的新代码:

And here is the new code that throws the error:

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV; #load Text::CSV module for parsing CSV
use LWP::Simple; #load LWP module for doing HTTP get

my $file = 'sid_swap.csv'; #CSV file to parse and load
my $csv = Text::CSV->new(); #create a new Text::CSV object

open (CSV, "<", $file) or die $!; #open CSV file for parsing

while (<CSV>) {
    if ($csv->parse($_)) {

        my @columns = $csv->fields(); #parse csv files and load into an array for each row
        my $newSID = $columns[0];
        my $oldSID = $columns[1];
        system("sed -i 's/\<row SERVICE_MENU_ID=\"$oldSID\"/\n$newSID\|/g' customer_data.txt");

    } else {
        my $err = $csv->error_input;
        print "Failed to parse line: $err";
    }
}
close CSV;

感谢您提供的任何帮助!

Thanks for any help you can provide!

推荐答案

要调试,将 system("sed ...") 更改为 die("sed ...");.你会看到你正在尝试执行

To debug, change system("sed ...") to die("sed ...");. You'll see you're what trying to execute

sed -i 's/<row SERVICE_MENU_ID="OLDSID"/
NEWSID|/g' customer_data.t

我猜 sed 不喜欢它的参数中间的实际换行符?这可以使用适当的转义来解决,但你的方法是......疯了.您正在为 CSV 的每一行处理整个文件!

I guess sed doesn't like actual newlines in the middle of its arguments? That can be fixed using proper escaping, but your approach is... insane. You're processing the entire file for each row of the CSV!

open(my $CSV, "<", $file) or die $!;    # Let's not use global vars!

my %fixes;
while (<$CSV>) {
    $csv->parse($_)
        or die "Failed to parse line: ".$csv->error_input."\n";

    my ($newSID, $oldSID) = $csv->fields();
    $fixes{$oldSID} = $newSID;
}

my $pat = join "|", map quotemeta, keys(%fixes);
my $re = qr/$pat/;

{
    local @ARGV = 'customer_data.txt';
    local $^I = '';  # "perl -i" mode
    while (<>) {
        s/<row SERVICE_MENU_ID="($re)"/\n$fixes{$1}|/g;
        print;
    }
}

一次搞定也解决了

1111,2222
2222,3333

1111,3333
2222,3333

这篇关于未终止的`s'命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆