
Let's say you have a file named example.txt that perhaps contains new lines and carriage returns. The cat command without any options or flags will just display the file content and will not show if the file contains new lines or carriage returns.
~]$ cat example.txt
Hello
World
How
are
you
today?
The -v flag or the --show-all can be used to display all characters. In this example, the $ character is used to display when each line ends.
~]$ cat --show-all example.txt
Hello$
World$
How$
are$
you$
today?$
In this example, the ^M character is used to represent carriage returns.
~]$ cat --show-all example.txt
Hello^M$
World^M$
How^M$
are^M$
you^M$
today?^M$
Dealing with newlines in sed can be tricky, because of how sed naturally deals with new lines. For example, let's say you have a file or variable that contains the text "Hello World". This text will end with a new line.
Hello\n
World\n
How\n
are\n
you\n
today?\n
When using sed to manipulate the file or varilable, sed will not do any sort of manipulation against the new line. For example, if you replace the word "World" with "Earth", the new line remains.
Hello\n
Earth\n
How\n
are\n
you\n
today?\n
Remove new lines
Almost always, xargs can be used to replace newlines with whitespace.
~]# cat file.txt | xargs
Hello World How are you today?
Or, using sed. In this example, new lines are replaced with a single whitespace.
- :label is a label. You could replace "label" with some other unique string of text.
- N gets the current line and the next line.
- $! means do not continue when the last line has been reached.
- b label means go to the label.
- s|\n| |g is the sed replacement command.
~]# sed ':label; N; $! b label; s|\n| |g' file.txt
Hello World How are you today?
Sometimes, the file command can be used to determine if a file has carriage returns. In this example, CRLF line terminators means the file most likely has carriage returns.
~]# file /path/to/example.py
/path/to/example.py: Python script, ASCII text executable, with CRLF line terminators
It's also probably a good idea to also replace with carriage returns (\r).
~]# sed ':label; N; $! b label; s|\r\n| |g' file.txt
Hello World How are you today?
And here is how you can replace new lines with the literal new line character \n.
~]# sed ':label; N; $! b label; s|\r\n|\\n|g' file.txt
Hello\nWorld\nHow\nare\nyou\ntoday?
Did you find this article helpful?
If so, consider buying me a coffee over at