Search a .log file and output results to csv in Python -


i use python search through log file multiple times using same grep statement (or equivalent of grep statement in python) , output results .csv file, appending search results each time.

my log file (small sample):

16:14:59.027003 - warn - cancel type: 100ms - id: 311yrsbj - on venue: abcd
16:14:59.027010 - warn - ack type: 25ms - id: 311yrsbl - on venue: efgh
16:14:59.027201 - warn - ack type: 22ms - id: 311yrsbn - on venue: ijkl
16:14:59.027235 - warn - cancel type: 137ms - id: 311yrsbp - on venue: mnop
16:14:59.027256 - warn - cancel type: 220ms - id: 311yrsbr - on venue: qrst
16:14:59.027293 - warn - ack type: 142ms - id: 311yrsbt - on venue: uvwx
16:14:59.027329 - warn - cancel type: 134ms - id: 311yrsbv - on venue: yz
16:14:59.027359 - warn - ack type: 75ms - id: 311yrsbx - on venue: abcd
16:14:59.027401 - warn - cancel type: 66ms - id: 311yrsbz - on venue: abcd
16:14:59.027426 - warn - cancel type: 212ms - id: 311yrsc1 - on venue: efgh
16:14:59.027470 - warn - cancel type: 89ms - id: 311yrsf7 - on venue: ijkl
16:14:59.027495 - warn - cancel type: 97ms - id: 311yrsay - on venue: ijkl

my grep statement looks this:
cat file.msg.log | grep abcd | awk '{print $14,$10,$5,$7}' | sort -t' ' -k4 -n -r | head -10 > output.csv

my results this:
abcd 321usdoe ack 58ms
abcd 32480jbm ack 55ms
abcd 32414dxp cancel 49ms
abcd 32480j98 cancel 39ms
abcd 32414c8y cancel 32ms
abcd 32yqhosn ack 30ms
abcd 321uscix ack 30ms
abcd 32414cd9 cancel 29ms
abcd 32yqt2id ack 20ms
abcd 32yqj7m3 ack 20ms

the results top ten results abcd ordered descending last field (58ms, 55ms, 49ms , on). run same grep statement efgh, ijkl, mnop , on , write results same .csv file. need output written separate columns, below.

column-a column-b column-c column-d
abcd 2sxrb6ab cancel 46ms
abcd 2sxrb6af cancel 45ms
abcd 2sxrb6i2 cancel 63ms
abcd 2sxrb6i3 cancel 103ms
efgh 2sxrb6i4 cancel 60ms
efgh 2sxrb6i7 cancel 60ms
ijkl 2sxrb6ie ack 74ms
ijkl 2sxrb6if ack 74ms
ijkl 2sxrb76s cancel 46ms
mnop vcxrqrs5 cancel 7651ms

code i've tried:

c = csv.writer(open("output.csv", "wb")) c.writerow(["column-a, column-b, column-c, column-d"]) collection = ['abcd', 'efgh', 'ijkl', 'mnop', 'qrst', 'uvwx', 'yz'] x in collection:     cat file.msg.log | grep x | awk '{print $14,$10,$5,$7}' | sort -t' ' -k4 -n -r | head -10 > output.csv 

can using grep on , on again, or should using different strategy. also, not sure if collection can used here. , i'm having trouble getting results own column. i'm beginner in python , haven't done coding since college (13 years ago). appreciated. thanks.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -