-2

Need some help sorting the results from .findall in python to convert the results in some readable format. I have a snippet giving me following output

[('Jul 11 11:25:51', 'ul 11 11:25:51', '', '', ''), ('', '', 'u2k', '', ''), ('', '', '', 'CBDCS2.CTP', ''), ('Jul 11 11:25:52', 'ul 11 11:25:52', '', '', ''), ('', '', 'u2k', '', ''), ('', '', '', 'WNDCS2.WTC', ''), ('Jul 11 11:25:53', 'ul 11 11:25:53', '', '', ''), ('', '', '', '', 'interface GigabitEthernet 2/0/44 '), ('', '', 'lqin', '', ''), ('', '', '', 'CASWNTHS151', ''), ('Jul 11 11:25:55', 'ul 11 11:25:55', '', '', ''), ('', '', 'u2k', '', ''), ('', '', '', 'asuspmk02', ''), ('Jul 11 11:25:55', 'ul 11 11:25:55', '', '', ''), ('', '', 'u2k', '', ''), ('', '', '', 'h_asuspmk01', ''), ('Jul 11 11:26:13', 'ul 11 11:26:13', '', '', ''), ('', '', '', 'cis1tow', ''), ('Jul 11 11:26:32', 'ul 11 11:26:32', '', '', ''), ('', '', '', '', 'show rpl route-policy RP-AKAMAI-LDV-OUT attachpoints '), ('', '', 'lyang', '', ''), ('', '', '', 'bgnzldv02', ''), ('Jul 11 11:26:32', 'ul 11 11:26:32', '', '', ''), ('', '', '', '', 'switchport voice vlan 462 '), ('', '', 'lqin', '', ''), ('', '', '', 'CASWNTHS151', ''), ('Jul 11 11:26:38', 'ul 11 11:26:38', '', '', ''), ('', '', '', '', 'copy running-config startup-config '), ('', '', 'lqin', '', ''), ('', '', '', 'CASWNTHS151', ''), ('Jul 11 11:26:57', 'ul 11 11:26:57', '', '', ''), ('', '', 'lqin', '', ''), ('', '', '', 'CASWNTHS151', '')]

Need output as

Jul 11 11:25:51   ul 11 11:25:51 u2k CBDCS2.CTP

Jul 11 11:25:51   ul 11 11:25:51 u2k WNDCS2.WTC

and so on

Snippet as below

p = re.compile(r'([J](\S+\W+\S+\W+\S+))|User=(\S\S+),|NetworkDeviceName=(\S\S+),|CmdAV=([^\<]*)')
    results = re.findall(p,output)
    file4.write(str(results))

input as follows

Jul 11 11:58:37 CSCOacs_TACACS_Accounting 0034182898 1 0 2016-07-11 TACACS+ Accounting with Command, ACSVersion=acs-5.6.0.22-B.225, ConfigVersionId=18, Device IP Address=10.125.89.1, CmdSet=[ CmdAV=show rpl route-policy RP-GGC-CTP-OUT attachpoints <cr> ], RequestLatency=0, Type=Accounting, Privilege-Level=0, Service=Login, User=lyang, Port=/dev/vty10, Remote-AVPair=priv-lvl=0, AcctRequest-Flags=Stop, Service-Argument=shell, AcsSessionID=imau03ftc/256670213/33719073, NetworkDeviceName=u2k NetworkDeviceGroups=All Devices:All 
Psidom
  • 209,562
  • 33
  • 339
  • 356
Saadi381
  • 55
  • 1
  • 2
  • 9
  • Please post your attempt to solve this, and in what way(s) it doesn't do the job. – Scott Hunter Jul 10 '16 at 23:56
  • as i mentioned the content saved in file4 is as above ; what i needed was Jul 11 11:25:51 ul 11 11:25:51 u2k CBDCS2.CTP Jul 11 11:25:51 ul 11 11:25:51 u2k WNDCS2.WTC nO Comma's and brackets) – Saadi381 Jul 10 '16 at 23:57
  • So you haven't tried to solve this at all. – Scott Hunter Jul 10 '16 at 23:59
  • I can see `User`, `NetworkDeviceName` and `CmdAV` in your regex, but those terms are not appearing in your string – Andrea Corbellini Jul 11 '16 at 00:00
  • i did ; i tried to make it str from list , i have tried making lines from input and then searching it ..but no luck – Saadi381 Jul 11 '16 at 00:00
  • Then show what you tried. And show what the output should be for all of the sample input. – Scott Hunter Jul 11 '16 at 00:01
  • @Andrea Corbellini ... yes as i dont want them and in regex they are used for matching string after that word – Saadi381 Jul 11 '16 at 00:02
  • here is the sample input Jul 11 11:58:37 CSCOacs_TACACS_Accounting 0034182898 1 0 2016-07-11 TACACS+ Accounting with Command, ACSVersion=acs-5.6.0.22-B.225, ConfigVersionId=18, Device IP Address=10.125.89.1, CmdSet=[ CmdAV=show rpl route-policy RP-GGC-CTP-OUT attachpoints ], RequestLatency=0, Type=Accounting, Privilege-Level=0, Service=Login, User=lyang, Port=/dev/vty10, Remote-AVPair=priv-lvl=0, AcctRequest-Flags=Stop, Service-Argument=shell, AcsSessionID=imau03ftc/256670213/33719073, NetworkDeviceName=u2k NetworkDeviceGroups=All Devices:All the output is what i have already shown – Saadi381 Jul 11 '16 at 00:04
  • @Saadi381 Please edit it into the question instead of commenting – Moon Cheesez Jul 11 '16 at 00:05
  • done; also guys i am concerned about the output ; kindly ignore regex if you find anything not matching the output , i just need an output without comma's and brackets – Saadi381 Jul 11 '16 at 00:07
  • You have *not* shown all of the output; there are 10 timestamps in your "snippet output", 8 of which are distinct. – Scott Hunter Jul 11 '16 at 00:08
  • Dont worry about the regex ; need to format the output – Saadi381 Jul 11 '16 at 00:09

2 Answers2

0

This code:

line = []
for d in results:
    if d[0] != '':
        if len(line)>0:
            print " ".join(line)
        line = []
    for dx in d:
        if dx != '':
            line.append( dx )
if len(line)>0:
    print " ".join(line)

produces this output when results is as shown in the top of the question:

Jul 11 11:25:51 ul 11 11:25:51 u2k CBDCS2.CTP
Jul 11 11:25:52 ul 11 11:25:52 u2k WNDCS2.WTC
Jul 11 11:25:53 ul 11 11:25:53 interface GigabitEthernet 2/0/44  lqin CASWNTHS151
Jul 11 11:25:55 ul 11 11:25:55 u2k asuspmk02
Jul 11 11:25:55 ul 11 11:25:55 u2k h_asuspmk01
Jul 11 11:26:13 ul 11 11:26:13 cis1tow
Jul 11 11:26:32 ul 11 11:26:32 show rpl route-policy RP-AKAMAI-LDV-OUT attachpoints  lyang bgnzldv02
Jul 11 11:26:32 ul 11 11:26:32 switchport voice vlan 462  lqin CASWNTHS151
Jul 11 11:26:38 ul 11 11:26:38 copy running-config startup-config  lqin CASWNTHS151
Jul 11 11:26:57 ul 11 11:26:57 lqin CASWNTHS151

Whether that is what the author wants is hard to tell from the ambiguous question.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • apologies if i couldnt make the question clear enough ; i am a newbie in this ..i will try your code and will let you know..thanks – Saadi381 Jul 11 '16 at 00:41
-1

Did you try formatting output that you already got?

p = re.compile(r'([J](\S+\W+\S+\W+\S+))|User=(\S\S+),|NetworkDeviceName=(\S\S+),|CmdAV=([^\<]*)')
results = re.findall(p,output)
file4.write('\n'.join([' '.join([str(i) for i in row]) for row in results]))
Kevin
  • 901
  • 1
  • 7
  • 15
  • I am glad it was useful. best wishes. – Kevin Jul 11 '16 at 00:23
  • This output doesn't look at all like the example in the question. – Scott Hunter Jul 11 '16 at 00:31
  • @scott Hunter ; i know but its a good starting point at least ; or if you have any better answer ; i would really appreciate if i can get more like what i required – Saadi381 Jul 11 '16 at 00:33
  • @ScottHunter Idea was to give a hint towards formatting output from the results he already got. It looks like he got the direction. Hope you see it as a positive outcome. – Kevin Jul 11 '16 at 00:52