Shell Scripting – Remove End of Line Characters from Stdout

shellshell-script

I have a script which outputs the following text. This is the output from a Netopia 2210-02 ADSL2 modem.

ADSL Line State:        Up
ADSL Startup Attempts:  1
ADSL Modulation:        DMT
ADSL Data Path:         Fast
Datapump Version:       DSP 7.2.3.0, HAL 7.2.1.0
SNR Margin:                   8.20        9.00 dB
Line Attenuation:            57.50       31.00 dB
Output Power:                17.09       12.34 dBm
Errored Seconds:                 0           0
Loss of Signal:                  0         476
Loss of Frame:                   0           0
CRC Errors:                  57921         416
Data Rate:                    2880        1024

How can I remove the end-of-line character for each line? I would like the output to look something like this (Yes it's ugly):

ADSL Line State:        Up ADSL Startup Attempts:  1 ADSL Modulation:        DMT ADSL Data Path:         Fast Datapump Version:       DSP 7.2.3.0, HAL 7.2.1.0 SNR Margin:                   8.20        9.00 dB Line Attenuation:            57.50       31.00 dB Output Power:                17.09       12.34 dBm Errored Seconds:                 0           0 Loss of Signal:                  0         476 Loss of Frame:                   0           0 CRC Errors:                  57921         416 Data Rate:                    2880        1024 

I tried some solutions like this, but they don't work:

# (This simply outputs the contents of the script, unmodified)
stefanl@hosta:~/Work/Cacti $ ./script | sed -e 's/$//'

I also tried using tr. I was expecting the following command to replace each newline character with the space character. This would take the multiple lines and combine them into one long single line. Instead, this only displays the last line of output. It seems to overwrite each subsequent line with the next line of output.

stefanl@hosta:~/Work/Cacti $ ./script | tr '\n' ' '
Data Rate:                    2880        1024stefanl@hosta:~/Work/Cacti $
stefanl@hosta:~/Work/Cacti $

Update:

Upon further examination, it looks like each line is preceded by a return character. This shows up as ^M when using less. So, I added two tr statements. One to delete newline characters, one to delete the return character.

./script | | tr -d '\n' | tr -d '\r'

Best Answer

sed won't work (easily) because it operates on lines one at a time; you could do it, but it would involve copying the whole input into the hold buffer

tr actually should work the way you pasted it; are you sure the newlines are \ns? You can simplify it a bit by deleting the newlines instead of converting them to spaces:

tr -d '\n'
Related Question