samedi 31 janvier 2015

sed: read whole file into pattern space without failing on single-line input


Reading a whole file into pattern space is useful for substituting newlines, &c. and there are many instances suggesting the following:



sed ':a;N;$!ba; [commands...]'


However, it fails if the input contains only one line.


As an example, with two line input, every line is subjected to the substitution command:



$ echo $'abc\ncat' | sed ':a;N;$!ba; s/a/A/g'
Abc
cAt


But, with single line input, no substitution is performed:



$ echo 'abc' | sed ':a;N;$!ba; s/a/A/g'
abc


How does one write a sed command to read all the input in at once and not have this problem?



Aucun commentaire:

Enregistrer un commentaire