Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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'
share|improve this question

2 Answers 2

up vote 10 down vote accepted

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'
share|improve this answer
    
Both tr '\n' ' ' and tr -d '\n' will wrap the line in a weird way. I updated my question to show the results. So, maybe I need to use tr, but I just need to figure out how to use use it properly. –  Stefan Lasiewski Oct 20 '10 at 18:26
    
Could you expand on what is weird about it? The reason your prompt is ending up on the same line as the last line of output is precisely because you have removed the '\n' at the end of the line. If you describe the end result you are going for, I'm sure someone will come up with the appropriate filter. –  Steven D Oct 20 '10 at 19:58
    
Ok, I guess I could be more clear. I was hoping to wrap these multiple lines into a single line. I would expect tr '\n' ' ' to remove the newline character and replace it with a space character, and therefore have one long line. –  Stefan Lasiewski Oct 21 '10 at 0:17
    
@Steven : I updated my question to discuss what I was expecting to see, and what was actually printed to the screen. –  Stefan Lasiewski Oct 21 '10 at 0:35
    
Hmm. Unless your output isn't ended with \n's this should give you the output you are expecting. Have you tried piping it into a file and then confirming that the line wrapping isn't something that your term is doing for you? –  Steven D Oct 21 '10 at 0:45

besides tr, some other methods you can use

awk

awk '1' ORS= file

shell

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

sed

sed -e ':a' -e 'N;s/\n/ /;ba' file
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.