Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a Perl one-liner for a Makefile so I can translate a literal \n to a newline?

Tags:

regex

perl

I need to escape \n so on output I really get newline or tab

$ perl -p -e 's/e/a/ge'

now I want each e to be substituted with \n

$ perl -p -e 's/e/\n/ge'

but even \n gives me an error.

this was a simplified example. In real script(makefile) I have

substitute := perl -p -e 's/@([^@]+)@/defined $$ENV{$$1} ? $$ENV{$$1} : $$1/ge'

and in target I have such a nice command

$(substitute) $< > $@

and if the input file for perl contains \n at output I will see it literally... I want to have real newline.

like image 983
Pablo Avatar asked Dec 29 '22 17:12

Pablo


1 Answers

Remove the e modifier and the substitution will work fine:

perl -p -e 's/e/\n/g' file

From perldoc perlop:

e   Evaluate the right side as an expression.

UPD: If you want to preserve it, put the escape sequence in double quotes:

perl -p -e 's/e/"\n"/ge' file
like image 193
Eugene Yarmash Avatar answered Dec 31 '22 05:12

Eugene Yarmash