Yes I know there are a number of questions (e.g. (0) or (1)) which seem to ask the same, but AFAICS none really answers what I want.
What I want is, to replace any occurrence of a newline (LF) with the string \n, with no implicitly assumed newlines... and this with POSIX only utilities (and no GNU extensions or Bashisms) and input read from stdin with no buffering of that is desired.
So for example:
- printf 'foo' | magicshould give- foo
- printf 'foo\n' | magicshould give- foo\n
- printf 'foo\n\n' | magicshould give- foo\n\n
The usually given answers, don't do this, e.g.:
- awk
 printf 'foo' | awk 1 ORS='\\ngivesfoo\n, whereas it should give justfoo
 so adds an\nwhen there was no newline.
- sed
 would work for justfoobut in all other cases, like:
 printf 'foo\n' | sed ':a;N;$!ba;s/\n/\\n/g'givesfoo, whereas it should givefoo\n
 misses one final newline.
 Since I do not want any sort of buffering, I cannot just look whether the input ended in an newline and then add the missing one manually.
 And anyway... it would use GNU extensions.
 sed -z 's/\n/\\n/g'
 does work (even retains the NULs correctly), but again, GNU extension.
- tr
 can only replace with one character, whereas I need two.
The only working solution I'd have so far is with perl:
perl -p -e 's/\n/\\n/'
which works just as desired in all cases, but as I've said, I'd like to have a solution for environments where just the basic POSIX utilities are there (so no Perl or using any GNU extensions).
Thanks in advance.
 
     
     
    