-1

On a Pi, in a text file like this

line1
line2
line3
...

how can I translate that to a file with just one line formatted like this

line1\n\line2\nline3\n......

NB The real file is 50MB and 200000 lines long

SimpleSi
  • 753
  • 1
  • 10
  • 22

2 Answers2

1

You can use sed

sed ':a;N;$!ba;s/\n/\\n/g' my.txt >> new_my.txt This will read the whole file in a loop, then replaces the newline(s) with a "\n" and store it in a new file.

Ashish Kumar
  • 593
  • 4
  • 12
0

With GNU sed you can:

sed -z -i -e 's/\n/\\n/g' file

replace all newlines for \n character. This can use some memory, as it can read the whole file into memory.

With awk you can print each line with \\n on the end:

awk '{printf "%s\\n", $0}'

You can use xargs to split the input on newlines and run printf:

cat file | xargs -d $'\n' printf '%s\\n'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111