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:

  1. select lines have $id_play 2nd field.
  2. 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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -