2
I need to search for and replace three consecutive newline characters in an input file and filter them out of an output file for a makefile rule on Centos 4. I am using GNU sed v4.1.2 and GNU make v3.82. I've tried variations on the following with no success thus far:
THREE_LINE_FEEDS := $(shell echo "\012\012\012")
SED_RULE := 's/'$(THREE_LINE_FEEDS)'//'
output.txt: input.txt
sed -r $(SED_RULE) input.txt > output.txt
Using the proposed perl, I get this issue in the shell (adapted from my make rule):
> cat input.txt | perl -e '$/ = undef; _ = <>; s/\n{3}//g; print;' > output.txt
Can't modify constant item in scalar assignment at -e line 1, near "<>;"
Execution of -e aborted due to compilation errors.
The perl is not working when inserted into my makefile rules, please see above updated question. – WilliamKF – 2012-07-28T15:49:02.617
When using
$
in makefiles they need to be protected, i.e. add an extra$
on there, soperl -e '$$/ = undef; $$_ = <>; s/\n{3}//g; print' input.txt > output.txt
should work. – Thor – 2012-07-28T15:52:42.067Yes, had figured that out, but still not working. Getting error: Can't modify constant item in scalar assignment at -e line 1, near "<>;" Execution of -e aborted due to compilation errors. Can you pipe into perl? Why is it executing -e? – WilliamKF – 2012-07-28T16:14:13.397
The error you're getting comes from this bit:
_ = <>;
, should be$_ = <>;
. Either ofperl -e ... infile
andcat infile | perl -e ...
works.-e
specifies the commandperl
should run, seeperlrun(1)
. – Thor – 2012-07-28T17:02:49.203Bingo, it works now, needed other $ doubled in the makefile. – WilliamKF – 2012-07-28T18:42:16.317
2
perl -0777pe 's/\n{3}//g'
is exactly the same.0777
puts Perl into file-slurp mode, andp
makes a read loop which prints – Peter.O – 2012-07-28T21:32:59.770Nice, didn't know about the
-0777
option. – Thor – 2012-07-28T22:02:45.663