Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl one-liner to remove trailing space from a file

Tags:

perl

I am calling a perl one-liner inside a Perl script.

The intention of the one-liner is to remove the trailing space from a file.

Inside the main perl script:

`perl -pi -e 's/\s+$//' tape.txt`;

Now it is throwing me an error Substitution replacement not terminated at -e line 2.

Where is it going wrong?

like image 221
Govind Kailas Avatar asked Dec 15 '22 18:12

Govind Kailas


1 Answers

It's because of the $/ (special variable) inside your main perl script. Note that variables are interpolated inside `` strings just like inside "" strings, and the fact that there are some single quotes in there doesn't change that. You need to escape that $:

`perl -pi -e 's/\s+\$//' tape.txt;`
like image 97
Dan Avatar answered Dec 21 '22 23:12

Dan