How can I write a single-line Perl for a Makefile so that I can translate the literal \ n to a new line?

I need to exit \ n, so in the output I really get a new line or tab

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

now i want each e to be replaced with \ n

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

but even \ n gives me an error.

it was a simplified example. In a 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 team

 $(substitute) $< > $@ 

and if the input file for perl contains \n in the output, I will see it literally ... I want to have a real new line.

+4
source share
4 answers

Remove the e modifier and the replacement 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 keep it, put the escape sequence in double quotes:

 perl -p -e 's/e/"\n"/ge' file 
+4
source

You do not need e in your regex:

 dpavlin@llin :~/private/home_html$ perl -p -e 's/e/\n/g' foedoe fo do 
+3
source

To interpolate things that themselves need interpolation (e.g. variables or screens), use double e and add quotation marks:

 perl -p -e 's/@([^@]+)@/q!"! . ( defined $ENV{$1} ? $ENV{$1} : $1 ) . q!"!/gee' 

This will not work if the substitution itself contains an unshielded "either $ or @", in which case you will need to perform backward kosin with a backward trace.

+3
source
 echo xeieio | perl -pe 's/(.)/$k=$1;$k=~s#e#\n#;$k/ge' 

outputs:

x
I
io

What it does ... for each character, assigns it $ k, starts a simple s / e / \ n / on $ k, and then prints $ k. (Also, as always, there is more than one way to do this)

0
source

Source: https://habr.com/ru/post/1309675/


All Articles