LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Change output format with SED - cannot get rid of CR/LF (https://www.linuxquestions.org/questions/programming-9/change-output-format-with-sed-cannot-get-rid-of-cr-lf-914197/)

philipz 11-18-2011 03:05 AM

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

syg00 11-18-2011 04:03 AM

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.

devUnix 11-18-2011 05:41 AM

tr and sed to break lines and format output
 
Quote:

Originally Posted by philipz (Post 4527318)
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!

philipz 11-18-2011 05:46 AM

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.

philipz 11-18-2011 06:06 AM

Quote:

Originally Posted by devUnix (Post 4527416)
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!

devUnix 11-18-2011 06:13 AM

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?

grail 11-18-2011 06:21 AM

I would be checking your router command to as normally you can tell it to not split over lines.

philipz 11-18-2011 06:39 AM

@ 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!

grail 11-18-2011 09:16 AM

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"'


All times are GMT -5. The time now is 03:39 AM.