Replace Newlines with Sed
This has bugged me for a while when useing TextMates ‘Filter Through Command…’ to replace all newlines in a selection. Somehow my mind always wants to use sed to do this although there are simpler ways to do it.
The following works with GNU sed but not with BSD sed on my Mac (although it should work given the documentation in the man page):
sed -n ':a;N;$!ba;s/\n//g;p' < infileWhat this does is the following:
- :a put label at the first position
- N Append the next line of input to the pattern space
- $!ba branch to label a, the ! tells it to apply the command to a non-selected pattern space and the $ addresses the last line of input
- s/\n//g substitutes a newline with a space
- p writes pattern space to standard output
If you can make this work with BSD sed please let me know.