Replace newlines with in a file in Linux bash

Home > Search
  by

Let's say file.txt contains newlines after text.

Hello
World
How
are
you
today?

 

The sed command can be used to replace the new lines with something else. In this example, new lines are replaced with a single whitespace.

  • :a and a are a label. You could replace a with some other label.
  • N gets the current line and the next line.
  • $! means do not continue when the last line has been reached.
  • ba means go to label a.
  • s|\n| |g is the sed replacement command.
~]# sed ':a;N;$!ba;s|\n| |g' file.txt
Hello World How are you today?


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments