LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2011, 03:05 AM   #1
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Rep: Reputation: 0
Change output format with SED - cannot get rid of CR/LF


All,

I am trying to parse the output of a router command with script. It works most of the time as this is the output format:

Code:
Mu650         11.230.132.112  Fa0/0.100     11.286.72.33    06 01BD 0F42  9124K
Fa0/0.100     11.286.72.33    Mu650         11.12.46.49     06 0FA6 01BB  5327K
Mu650         11.242.100.227  Fa0/0.101     11.286.72.12    06 05DF 056A  2008K
If the first column is too wide the router adds some non-printable characters and spaces:
Code:
Se0/0/0:0.1111
              11.12.215.49    Fa0/0.100     11.243.112.100  06 1F4E 0954    10M
Se0/0/0:0.1111
              11.242.100.149  Fa0/0.101     11.243.121.131  06 05DF 10CD   838K
Se0/0/0:0.1111
              11.231.247.26   Fa0/0.100     11.243.122.145  06 1F91 0AE4   317K

I tried with SED to replace the CR/LF/spaces sequence but was unsuccesful so far. Instead of the above I want this:

Code:
Se0/0/0:0.1111 11.12.215.49    Fa0/0.100     11.243.112.100  06 1F4E 0954    10M
Se0/0/0:0.1111 11.242.100.149  Fa0/0.101     11.243.121.131  06 05DF 10CD   838K
Se0/0/0:0.1111 11.231.247.26   Fa0/0.100     11.243.122.145  06 1F91 0AE4   317K
Any ideas on how to accomplish this?

Thanks,

Phil
 
Old 11-18-2011, 04:03 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Shouldn't be too hard - are you sure there isn't a newline in there as well ?. sed is a stream editor - newlines mess with it.
 
Old 11-18-2011, 05:41 AM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
tr and sed to break lines and format output

Quote:
Originally Posted by philipz View Post
I tried with SED to replace the CR/LF/spaces sequence but was unsuccesful so far. Instead of the above I want this:

Code:
Se0/0/0:0.1111 11.12.215.49    Fa0/0.100     11.243.112.100  06 1F4E 0954    10M
Se0/0/0:0.1111 11.242.100.149  Fa0/0.101     11.243.121.131  06 05DF 10CD   838K
Se0/0/0:0.1111 11.231.247.26   Fa0/0.100     11.243.122.145  06 1F91 0AE4   317K
Any ideas on how to accomplish this?

Thanks,

Phil
Phil:


If you can give me the command that you are using, may be I can do better than what I have done to help you. I copied your command's output to a file:

Code:
$ cat > Test
Se0/0/0:0.1111
              11.12.215.49    Fa0/0.100     11.243.112.100  06 1F4E 0954    10M
Se0/0/0:0.1111
              11.242.100.149  Fa0/0.101     11.243.121.131  06 05DF 10CD   838K
Se0/0/0:0.1111
              11.231.247.26   Fa0/0.100     11.243.122.145  06 1F91 0AE4   317K
and then did this:

Code:
$ cat Test | tr '\n' ' ' | sed 's/Se/\nSe/g'

Se0/0/0:0.1111               11.12.215.49    Fa0/0.100     11.243.112.100  06 1F4E 0954    10M
Se0/0/0:0.1111               11.242.100.149  Fa0/0.101     11.243.121.131  06 05DF 10CD   838K
Se0/0/0:0.1111               11.231.247.26   Fa0/0.100     11.243.122.145  06 1F91 0AE4   317K
So, I broke down all the input into single line containing of words/fields (see tr command above) and then broke them apart (see sed command above) to get the desired output as shown above.

Does that help you? (I am assuming that your lines with "Se". We can use the last field also that contains 10M/K etc.)

Cheers!

Last edited by devUnix; 11-18-2011 at 05:44 AM.
 
Old 11-18-2011, 05:46 AM   #4
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Well this is what I see when I show non-printable characters with cat:
Code:
Se0/0/0:0.1111^M$
              10.6.156.4      Fa0/0.111     11.243.112.115  06 0626 01BD   168K^M$
Fa0/0.111     11.243.112.115  Se0/0/0:0.1111^M$
                                            10.6.156.4      06 01BD 0626   165K^M$
Fa0/0.502     11.243.121.158  Se0/0/0:0.1111^M$
                                            11.231.245.19   06 086D 01BD   138K^M$
When pasting the result in Notepad++ it shows a CR and and LF afterwards. I am not sure how to capture what it is exactly in a syntax that is suitable for sed.
 
Old 11-18-2011, 06:06 AM   #5
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by devUnix View Post
Phil:


If you can give me the command that you are using, may be I can do better than what I have done to help you. I copied your command's output to a file:

and then did this:

Code:
$ cat Test | tr '\n' ' ' | sed 's/Se/\nSe/g'

Se0/0/0:0.1111               11.12.215.49    Fa0/0.100     11.243.112.100  06 1F4E 0954    10M
Se0/0/0:0.1111               11.242.100.149  Fa0/0.101     11.243.121.131  06 05DF 10CD   838K
Se0/0/0:0.1111               11.231.247.26   Fa0/0.100     11.243.122.145  06 1F91 0AE4   317K
So, I broke down all the input into single line containing of words/fields (see tr command above) and then broke them apart (see sed command above) to get the desired output as shown above.

Does that help you? (I am assuming that your lines with "Se". We can use the last field also that contains 10M/K etc.)

Cheers!
The tr did not work; if I append that to my script (./myscript.sh 10.11.12.13 | tr '\n' ' ') then the output is empty...
Anyway, the lines can start with all sort of things (Se/Fe/GigE/...) and the last field can be any number or end with M or K as well... I guess it would be easier to replace the obsolete spaces in the output?
I am willing to share the script itself but it is a TCL script logging on to a router and issueing a command so not really necessary to show.

Thanks for the help so far!
 
Old 11-18-2011, 06:13 AM   #6
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Philip:

Try this:

Code:
cat Test | tr '\n' ' ' | sed 's/[0-9][MK]/&\n/g' | sed 's/^\s//'
Se0/0/0:0.1111               11.12.215.49    Fa0/0.100     11.243.112.100  06 1F4E 0954    10M
Se0/0/0:0.1111               11.242.100.149  Fa0/0.101     11.243.121.131  06 05DF 10CD   838K
Se0/0/0:0.1111               11.231.247.26   Fa0/0.100     11.243.122.145  06 1F91 0AE4   317K
My shift is over! I will check it later on. Can you share the router command you are using?
 
Old 11-18-2011, 06:21 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
I would be checking your router command to as normally you can tell it to not split over lines.
 
Old 11-18-2011, 06:39 AM   #8
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
@ devUnix:
This is the script:
Code:
#! /usr/local/bin/expect
set host [lindex $argv 0]

spawn telnet $host
expect "Username:"
send "myname\r"
expect "Password:"
send "mypassword\r"
expect ">"
send "show ip flow top-talkers\r"
expect ">"
send "exit\r"
close
I think that the main problem is that tr does not give a result, see the difference:

Code:
[root@myBSD]# ./toptalkers.sh 11.21.12.13
MyRouter>show ip flow top-talkers

SrcIf         SrcIPaddress    DstIf         DstIPaddress    Pr SrcP DstP Bytes
Fa0/0.111     11.243.112.120  Se0/0/0:0.2345
                                            11.280.128.163  06 2152 0872    13M
Fa0/0.111     11.243.112.120  Se0/0/0:0.2345
                                            11.280.128.163  06 2152 087E    11M
Se0/0/0:0.2345
              11.280.128.163  Fa0/0.111     11.243.112.120  06 0872 2152   650K


Code:
[root@myBSD]# ./toptalkers.sh 11.21.12.13 | tr '\n' ' '
MyRouter>[root@myBSD]#
@grail: I am not aware of a Cisco router command like that but that would solve my issue ofcourse; if you can find it I am more than interested!
 
Old 11-18-2011, 09:16 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
In the future it might be advisable to tell us you are using expect, telnet and then sending a cisco command (all of which may have some impact) and then finally that you are connecting
in a Windows interface, which of course use \n\r instead of just \n.

So maybe something like:
Code:
./script.sh | awk 'BEGIN{RS="[\n\r]+";OFS="\t"}{$1=$1}ORS=(NR%2)?"\t":"\n"'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
How do I get rid of surplus comment lines with sed Jykke Linux - General 3 02-14-2011 06:07 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
Best video format for YouTube upload and can Recordmydesktop output that format? linus72 Linux - Software 6 12-21-2009 03:53 PM
change output format for df baddah Programming 3 04-21-2008 08:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration