0

I am looking for a way to convert CRLF into a \n. Exemple :

What i have :

Line 1
Line 2

What i expect :

Line 1\nLine 2

I have a application that hates CRLF LF and end of line ?

Looking to do it in shell script or java application at worse.

Thank you

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

3 Answers3

1

This could be solved with a second slash before the n to escape the first one.

Your code with a print statement would look something like this:

System.out.println("Line 1\\nLine 2");
Syzygyn
  • 58
  • 6
0

Shell could be much faster really depending on how big the file is.

If you just want to change to say a "|"

cat file.txt | tr '\n' '|'

Output:

Line1|Line2|

if you must convert to a string (rather than one char)

cat file.txt | tr '\n' '|'  | sed s/\|/##/g

Output:

Line1##Line2##
A G
  • 297
  • 1
  • 7
0

Using GNU awk:

$ awk 'BEGIN{RS="\r?\n";ORS="\\n"}1' file

Output:

Line 1\nLine 2\n$

Works with mawk and Busybox awk, too.

James Brown
  • 36,089
  • 7
  • 43
  • 59