perl - Sed: syntax error with unexpected "(" -
i've got file.txt
looks this:
c00010018;1;17/10/2013;17:00;18;920;113;none c00010019;1;18/10/2013;17:00;18;920;0;none c00010020;1;19/10/2013;19:00;18;920;0;none
and i'm trying 2 things:
- select lines have
$id_play
2nd field. - replace
;
-
on lines.
my attempt:
#!/usr/bin/perl $id_play=3; $input="./file.txt"; $result = `sed s@^\([^;]*\);$id_play;\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\);\([^;]*\)\$@\1-$id_play-\2-\3-\4-\5-\6@g $input`;
and i'm getting error:
sh: 1: syntax error: "(" unexpected
why?
you have escape @
characters, add 2 backslashes in cases (thanks ysth!), add single quotes between sed , make filter lines. replace this:
$result = `sed 's\@^\\([^;]*\\);$id_play;\\([^;]*\\);\\([^;]*\\);\\([^;]*\\);\\([^;]*\\);\\([^;]*\\);\\([^;]*\\)\$\@\\1-$id_play-\\2-\\3-\\4-\\5-\\6-\\7\@g;tx;d;:x' $input`;
ps. trying can achieved in more clean way without calling sed
, using split. example:
#!/usr/bin/perl use warnings; use strict; $id_play=3; $input="file.txt"; open (my $in,'<',$input); while (<$in>) { @row=split/;/; print join('-',@row) if $row[1]==$id_play; } close $in;
Comments
Post a Comment