Accounts - How to replace newline using sed, Sed Command replace newline

How to replace newline using sed

How can I replace a newline (\n)?

 sed ':a;N;$!ba;s/\n/ /g' filename
1) :a create a label 'a'
2) N append the next line to the pattern space
3) $! if not the last line, ba branch (go to) label 'a'
4) s substitute, /\n/ regex for new line, / / by a space, /g global match (as many times as it can) sed will loop through step 1 to 3 until it reach the last line, getting all lines fit in the pattern space where sed will substitute all \n characters

Alternatives:

All alternatives, unlike sed will not need to reach the last line to begin the process. These might be little slow

 # while read line; do printf "%s" "$line "; done < file

 # perl -p -e 's/\n/ /' file

 # tr '\n' ' ' < file

In order to replace all newlines with spaces using awk, without reading the whole file into memory
 # awk '{printf "%s ", $0}' inputfile
If you want a final newline:
 # awk '{printf "%s ", $0} END {printf "\n"}' inputfile
You can use a character other than space
 # awk '{printf "%s|", $0} END {printf "\n"}' inputfile

The topic on Accounts - How to replace newline using sed is posted by - Guru

Hope you have enjoyed, Accounts - How to replace newline using sedThanks for your time

Tech Bluff