Mark
Mark

Reputation: 11

'sed' usage in perl script error

I have the following line in a Perl script:

my $temp = `sed 's/ /\n/g' /sys/bus/w1/devices/w1_bus_master1/10-000802415bef/w1_slave | grep t= | sed 's/t=//'`;

Which throws up the error: "sed: -e expression #1, char 2: unterminated `s' command"

If I run a shell script as below it works fine:

temp1=`sed 's/ /\n/g' /sys/bus/w1/devices/w1_bus_master1/10-000802415bef/w1_slave | grep t= | sed 's/t=//'`
echo $temp1

Anyone got any ideas?

Upvotes: 1

Views: 348

Answers (2)

user507077
user507077

Reputation:

Perl interpretes your \n as a literal newline character. Your command line will therefore look something like this from sed's perspective:

sed s/ /
/g ...

which sed doesn't like. The shell does not interpret it that way.

The proper solution is not to use sed/grep in such a situation at all. Perl is, after all, very, very good at handling text. For example (untested):

use File::Slurp;
my @lines = split m/\n/, map { s/ /\n/g; $_ } scalar(read_file("/sys/bus...));
@lines    = map { s/t=//; $_ } grep { m/t=/ } @lines;

Alternatively escape the \n once, e.g. sed 's/ /\\n/g'....

Upvotes: 4

simbabque
simbabque

Reputation: 54381

You need to escape the \n in our first regular expression. The backtick-operator in perl thinks it is a control-character and inserts a newline instead of the string \n.

                     |
                     V
my $temp = `sed 's/ /\\n/g' /sys/bus/ # ...

Upvotes: 1

Related Questions