Bootstrap FreeKB - Bash (Scripting) - new lines \n and carriage returns \r
Bash (Scripting) - new lines \n and carriage returns \r

Updated:   |  Bash (Scripting) articles

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

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 Buy Me A Coffee



Comments


Add a Comment


Please enter de8342 in the box below so that we can be sure you are a human.