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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

complex file parsing using strfind sLine, etc.

Status
Not open for further replies.

malium

ISP
Jun 26, 2003
23
0
0
US
I have an existing script someone helped me with about a year ago that parses a file and creates a summary of key info off a few lines. I want to do something similar to compile a report of certain fields from a bigger chunk of data where the data is not on sequential lines.

I've successfully modified the original script for some things similar to the initial use but this is getting too complicated for me to figure out how to pull the info I want from many lines. The original script is attached below. I could never figure out how to add an additional strfind to get a string from anything past the next line.

Desired output is a file of lines that are comma delimited that compiles multiple instances of this data from the source example below.

OUTPUT FILE SCHEME:
"SiteName1", "23:48:28 UTC Sun Jul 4 2004", "serial 3/0", "txload 20/255", "rxload 42/255", "30 second input rate 260000 bits/sec, 39 packets/sec", "30 second output rate 124000 bits/sec, 36 packets/sec", "1244774 packets input, 837043862 bytes", "1164989 packets output, 311815097 bytes", "0 carrier transitions"
"SiteName2", "23:48:28 UTC Sun Jul 4 2004", "serial 3/0", "txload 20/255", "rxload 42/255", "30 second input rate 260000 bits/sec, 39 packets/sec", "30 second output rate 124000 bits/sec, 36 packets/sec", "1244774 packets input, 837043862 bytes", "1164989 packets output, 311815097 bytes", "0 carrier transitions"

...
strings:
"SiteName", "Time", "Interface", "txload", "rxload", "InputRate", "OutputRate", "PacketsIn", "PacketsOut", "Transitions"

I don't need quotes as a delimiter, comma alone is fine. I just added them to make it easier to see the strings I want, and I don't care if the packets and bytes are separated by a comma within the string as is shown or if they're broken into two strings.

I want to pull this from a file with many instances of data in this example format below.

The Sitename will vary and the interface name will vary. Everything will be consistently in this format with the same text always preceding the data I want to grab.

Any help or pointers would be welcomed, especially on how I can do multiple strfinds (do I need multiple ifs?) or how I increment a set number of lines to the next line I want data from.

Thanks in advance.

Sitename#show calendar
23:48:28 UTC Sun Jul 4 2004
Sitename#show interface serial 3/0
Serial3/0 is up, line protocol is up
Hardware is M4T
Internet address is xxx.xxx.xxx.186/30
MTU 1500 bytes, BW 1544 Kbit, DLY 20000 usec,
reliability 255/255, txload 20/255, rxload 42/255
Encapsulation FRAME-RELAY IETF, crc 16, loopback not set
Keepalive set (10 sec)
Restart-Delay is 0 secs
LMI enq sent 10963, LMI stat recvd 10963, LMI upd recvd 0, DTE LMI up
LMI enq recvd 0, LMI stat sent 0, LMI upd sent 0
LMI DLCI 0 LMI type is ANSI Annex D frame relay DTE
Broadcast queue 0/64, broadcasts sent/dropped 0/0, interface broadcasts 0
Last input 00:00:00, output 00:00:00, output hang never
Last clearing of "show interface" counters 1d06h
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: fifo
Output queue :0/40 (size/max)
30 second input rate 260000 bits/sec, 39 packets/sec
30 second output rate 124000 bits/sec, 36 packets/sec
1244774 packets input, 837043862 bytes
, 0 no buffer
Received 0 broadcasts, 0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
1164989 packets output, 311815097 bytes, 0 underruns
0 output errors, 0 collisions, 0 interface resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions DCD=up DSR=up DTR=up RTS=up CTS=up




;example of similar working script that parses data from a capture file to another a file

proc main
string sLine
string sAddr1, sAddr2
string sOutput

if fopen 0 "c:\router_data\showRun_all.txt" READ TEXT
fopen 1 "c:\router_data\all_provisioned.txt" CREATE TEXT
while not feof 0
fgets 0 sLine
if strfind sLine "ip dhcp pool"
strtok sAddr1 sLine "-" 2
fgets 0 sLine
strtok sAddr2 sLine " " 2
strfmt sOutput "%s %s" sAddr1 sAddr2
fputs 1 sOutput
endif
endwhile
endif
endproc


; example of my half-baked draft of what I am looking for, at least as far as the start and end of it go:


proc main
string sLine
string SiteName, Time, Interface, TxLoad, RxLoad, InputRate, OutputRate, PacketsIn, PacketsOut, Transitions
string sOutput

if fopen 0 "c:\router_data\monitor_raw.txt" READ TEXT
fopen 1 "c:\router_data\monitor_summary.txt" CREATE TEXT
while not feof 0
fgets 0 sLine
if strfind sLine "????"

big gaps here in my understanding how to find various strings on various lines

strfmt sOutput "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" SiteName Time Interface TxLoad RxLoad InputRate OutputRate PacketsIn PacketsOut Transitions
string sOutput
fputs 1 sOutput
endif
endwhile
endif
endproc
 
If you want to be able to search for multiple strings in the same line of data, you'll need to either use multiple strfind commands one after another, or you could use a function I wrote that will search for multiple substrings in the string passed into the function. However, it may not work well for you in its current incarnation as it only indicates if one of the strings was found, not which particular string. I'll see if I can expand this a bit later this week to give it more flexibility. This way you could check each line of the text file against the list of strings you are interested in with one function call instead of multiple strfind commands.


aspect@aspectscripting.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top