I need to parse stdin in the following way:
(1) all newlines characters must be substituted with \n
(a literal \
followed by n
)
(2) nothing else should be performed except the previous
I chose awk
to do it, and I would like an answer that uses awk
if possible.
I came up with:
echo -ne "A\nB\nC" | awk '{a[NR]=$0;} END{for(i=1;i<NR;i++){printf "%s\\n",a[i];};printf "%s",a[NR];}'
But it looks cumbersome.
Is there a better / cleaner way?
With awk:
echo -ne "A\nB\nC" | awk 'BEGIN{FS="\n"; OFS="\\n"; RS=ORS=""} {$1=$1}1'
Output:
A\nB\nC
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR