Skip to content

Instantly share code, notes, and snippets.

@somada141
Created March 10, 2015 21:34
Show Gist options
  • Save somada141/dce96cddb1f93161ee3f to your computer and use it in GitHub Desktop.
Save somada141/dce96cddb1f93161ee3f to your computer and use it in GitHub Desktop.
Replace line breaks with spaces in the Bash #linux #bash #shell #string #file

By piping input, e.g., cat <some_file>, to the following command one can replace all line breaks with spaces

sed ':a;N;$!ba;s/\n/ /g'

This will read the whole input, e.g., file, in a loop and then replace the newline(s) with a space.

Explanation:

  • create a label via :a
  • append the current and next line to the pattern space via N
  • if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)).
  • finally the substitution replaces every newline with a space on the pattern space (which is the whole file).

source: http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment