Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join Multiple Lines Into A Single Line 3

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
I need the help of all Unix Guru out there.

I have this output from my Cisco router:

Cisco Internetwork Operating System Software
IOS (tm) C2600 Software (C2600-TELCO-M), Version 12.2(8)T10, RELEASE SOFTWARE (fc1)
TAC Support: Copyright (c) 1986-2003 by cisco Systems, Inc.
Compiled Fri 30-May-03 05:50 by kel

I tried using this command to combine all the lines into a single line:
sed -e :a -e '$!N;s/\n/ /;t a'

But it gave me an incomplete output. I don't understand the actual sed command (I just got it from one of the thread replies here). That's why I can't really modify it to format my data.

Any help will be appreciated.
 
And what about this ?
tr '\012' ' ' < /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

Thanks for your quick reply.

I got this error when I try to use the command you provided:

cat file | tr '\012' ' '
traceroute: " " bad value for packet length

Do you know why it gave that error?

Hope you can provide more help.
 
what's about

awk '{printf($0)}' input_file > output_file

Chacal, Inc.[wavey]
 
Seems you have an alias named tr !
(unalias tr; tr '\012' ' ' < file)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi Chacalinc,

It gave an incomplete output like this:

Compiled Fri 30-May-03 05:50 by kelstems, Inc.on 12.2(8)T10, RELEASE SOFTWARE (fc1)

The other lines are missing ....

by the way these are the original lines:

Cisco Internetwork Operating System Software
IOS (tm) C2600 Software (C2600-TELCO-M), Version 12.2(8)T10, RELEASE SOFTWARE (fc1)
TAC Support: Copyright (c) 1986-2003 by cisco Systems, Inc.
Compiled Fri 30-May-03 05:50 by kel

Thanks .. if you can provide other ways .. I am willing to try
 
Seems like CrLf end of line:
\tr '\012\015' ' ' < file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
it's weird, the post I gave you works ok for me (awk solution), even I used the PHV's solution and it works ok too. (using your data of course)

Cheers.

Chacal, Inc.[wavey]
 
Even the solution in the original question works fine for me; PHV's guess could be right, CR/LF line terminators or some other invisible characters involved...

Annihilannic.
 
Hi Annihilannic & PHV,

Your suggestions are correct, eventually I know what is causing the formatting problem. For some reasons (this is the first time I experience this scenario), the file (Cisco device system description SNMP data) I generated using the script on my server at work somehow has a different format.

I used the commands you guys suggested using my cygwin at home and it worked! The difference is that I didn't use my original file but instead I copy the file I will format from Tek-Tips webpage and paste it direct to a notepad. That gave me the idea to inspect the original file (from work) using "vi". I saw there ^M at the end of each line. So I inserted "dos2unix" command first before adding the commands you suggested and it worked! Lesson for me really ... I spent at least 3 hours asking and researching over the net!!!

I am really glad there are people here like you who makes my life at work a lot easier !!! A MILLION THANKS !!!

Hope you won't get tired answering my questions here ...

Cheers!


 
Well, then PHV's solution

Code:
tr '\015\012' '  ' <file

or

tr '\r\n' '  ' <file

should work without the dos2unix first - notice though that there are 2 spaces in the 2nd string. tr does a character translation:

1st char of 1st string (\r or \015 - shown as ^M in vi) is translated to the 1st character of 2nd string (here that is a space)

2nd char of 1st string (\n or \012 - newline) is translated to the 2nd char of 2nd string (here that is also a space).

Of course that would put two spaces instead of one between each line...

See also man page for tr


HTH,

p5wizard
 
p5wizard,

I was about to ask PHV to explain to me the "\tr '\012\015' ' '" command .. good thing you answered it already. I have no idea why it isn't working without dos2unix command ... I would like to understand this topic better but for now I am quuite satisfied, finally I managed to finish my script and was able to provide the report to my manager :)

mrn .. thanks for the help as well :)

Cheers!
Maria
 
Well, in PHV's post it is marked as \tr, perhaps if you copied/pasted that command literally it doesn't work? Allthough I wouldn't see why not.

Also it needs 2 spaces in the second tr string as I said earlier. Perhaps your tr doesn't understand '\015\012'. Did you try tr '\n\r' ' '

All academic now, I know, but still curious.



HTH,

p5wizard
 
\tr is to use the tr command instead of the tr alias.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top