-1

I have a file like this:

This
    file
has
newlines
and
    tabs

and I want to generate something that looks like this:

This\n\tfile\nhas\nnewlines\nand\n\ttabs

How can I easily get this output?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
YulkyTulky
  • 886
  • 1
  • 6
  • 20

1 Answers1

1

I just added an answer to Replace newlines with literal \n that works here too.
Using the "new" -z option you can do

sed -z 's/\t/\\t/g;s/\n/\\n/g' file

or

sed -z "s/\t/\\\t/g;s/\n/\\\n/g" file
Walter A
  • 19,067
  • 2
  • 23
  • 43