21. JANUAR 2019
Menu
Importdateien korrigieren, Assoziative Arrays unter AWK
Import-Dateien welche Daten im Key-Value Verfahren gespeichert haben können mit diesem Code zB. die einzelnen Felder (Reihenfolge oder Anzahl) umstrukturieren. Der Code funktioniert auch dann wenn nicht immer alle Felder gesetzt wurden. In diesem Falle wird ein leerer Eintrag erzeugt.
Die Perl-Anweisung korrigiert Leerzeichen zwischen Anführungszeichen zu Unterstrichen damit das AWK-Skript die Werte besser aus den Daten extrahieren kann.
#!/bin/bash echo "date;time;logid;type;subtype;level;vd;srcip;srcname;srcport;srcintf;dstip;dstport;dstinf;poluuid;sessionid;proto;action;policyid;dstcountry;srccountry;trandisp;service;appid;app;appcat;apprisk;applist;appact;duration;sentbyte;rcvdbyte;sentpkt;rcvdpkt;vpn;vpntype;utmaction;countapp;utmref" cat $1 | perl -pe 'sub c{$s=shift;$s=~s/ /_/g;$s}s/".*?"/c$&/ge' | awk -F" " '{ split("",arr) for(i=1; i<=NF; i++){ a=index($i, "=") if(a != 0){ key=substr($i,1,a-1) val=substr($i,a+1) # gsub(/^"*/,"",key) # gsub(/^"*/,"",val) arr[key]=val } } # print out the desired fields print arr["date"]"; "arr["time"]"; "arr["logid"]"; "arr["type"]"; "arr["subtype"]"; "arr["level"]"; "arr["vd"]"; "arr["srcip"]"; "arr["srcname"]"; "arr["srcport"]"; "arr["srcintf"]"; "arr["dstip"]"; "arr["dstport"]"; "arr["dstinf"]"; "arr["poluuid"]"; "arr["sessionid"]"; "arr["proto"]"; "arr["action"]"; "arr["policyid"]"; "arr["dstcountry"]"; "arr["srccountry"]"; "arr["trandisp"]"; "arr["service"]"; "arr["appid"]"; "arr["app"]"; "arr["appcat"]"; "arr["apprisk"]"; "arr["applist"]"; "arr["appact"]"; "arr["duration"]"; "arr["sentbyte"]"; "arr["rcvdbyte"]"; "arr["sentpkt"]"; "arr["rcvdpkt"]"; "arr["vpn"]"; "arr["vpntype"]"; "arr["utmaction"]"; "arr["countapp"]"; "arr["utmref"] }'
Ein Auszug aus den Quelldaten sieht ungefähr wie folgt aus:
date=2017-03-02 time=14:02:02 logid=0000000013 type=traffic subtype=forward level=notice vd=root srcip=104.131.176.5 srcport=42979 srcintf="wan1" dstip=213.185.179.167 dstport=443
Keywords: linux, shell, script, awk, perl, csv, import
Dokumenten-ID: kb/2b1c6602-9eb9-4b64-bffb-61023e58d532