Search code examples
parsingawksedcut

How parse line of NAT log and make a specific CSV output


I have a NAT device that makes NAT logs like this:

Dec 13 18:12:59 172.16.1.1 "LSN_DELETE""192.168.100.1%101:11921""TCP""100.100.100.100%101:11921""104.111.224.226:80""1481639868477""327573"

I want to make a csv file by converting each line of this file to:

192.168.100.1,11921,TCP,100.100.100.100,11921,104.111.224.226,80,1481639868477,327573

I know that I can read the file line by line and convert it using cut command but reading file line by line is very slow and the file is very huge (5 million lines). I want to convert it by awk or sed that are more fast. could someone please help me to do that?


Solution

  • In awk, a partial solution (you'll have to add fields in the order you like):

    gawk -F '[^.[:digit:][:alpha:]]*' -v OFS=, '{print $9,$15,$12}' file
    

    That is, assuming the log is in the file named file:

    • It breaks each line up using the field separator (-F), where is assumes (and this may not be 100% depending on your data) that a field any number of contiguous periods, digits, and alpha characters.
    • The output field separator (OFS) is set to comma
    • The chosen fields are printed, rearranged.

    No guarantees that this will be faster than what you were doing before.