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 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!
To debug, change
system("sed ...")
todie("sed ...");
. You'll see you're what trying to executeI 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!Doing it in one pass also solve the problem of
being the same as