awk one liners into a nice script

Hi All,

I got some awk one liners, how can i split it all into a nice script?

Got these:

gzcat capgw0.log-201308161376632741.gz | sed -n '/2013-08-16 05:56:/,/2013-08-16 05:58:/p' > timebased.log
awk -F":|," 'FNR==NR && /INFO  - AId:/ {a[$6$8]=$0;next} END {for (i in a) print i "|" a}' timebased.log > t1
awk  '/<?xml version/ {f=1} /<\/iSig>/ {f=0;print $0 "\n" } f' timebased.log > t2
awk -F\| 'FNR==NR {a[$1]=$2;next} FNR==1 {RS="\n\n"} { for (i in a) {if ($0~i) {print a $0 > i".log";close(i".log")}}}'  t1 t2

I want to make from these lines a real, nice looking script. Can anybody help me?

Thanks!

You mean cosmetically good looking ???

Yes! :slight_smile:

A nice script, those 4 lines into a "big" script.

As long as we do not have input data and desired output data, its difficult to shorten them.

These short bits of awk code are doing a fair bit. I'm not sure there's a simpler, trivial solution; if it works well I'd be tempted to leave well enough alone!

But without seeing the intent it's difficult to guess :slight_smile:

If these files are temporary files, you should be saving them in names like /tmp/$$-timebased.log to protect them from being stomped on by multiple instances of the program.

Do you mean like so (a pretty print, give or take):

gzcat capgw0.log-201308161376632741.gz | sed -n '/2013-08-16 05:56:/,/2013-08-16 05:58:/p' > timebased.log

awk     'FNR==NR && /INFO  - AId:/ {a[$6$8]=$0;next}
         END            {for (i in a) print i "|" a}
        '  FS=":|," timebased.log > t1

awk     '/<?xml versio/ {f=1}
         /<\/iSig>/     {f=0; print $0 "\n" }
         f
        ' timebased.log > t2

awk     'FNR==NR        {a[$1]=$2; next}
         FNR==1         {RS="\n\n"}   
                        { for (i in a)
                                {if ($0~i)      {print a $0 > i".log"; close(i".log")
                                                }
                                }
                        }
        ' FS="|"  t1 t2
1 Like

Works great! Thanks!

Another problem:

I got these 2 another lines, but these are processing different two logs, how can i add this to my main script?

awk -F'[]:]' '/INFO/ {print > ( $7"_moc.txt" )}' file
awk -F'[]:]' '/INFO/ {print > ($5_crce.txt")}' file

Can i do this with a for loop like this?
It gives me an error.

I got this now:

#!/bin/bash

gzcat capgw0.log-201308161376632741.gz | sed -n '/2013-08-16 05:56:/,/2013-08-16 05:58:/p' > timebased.log

awk     'FNR==NR && /INFO  - AId:/ {a[$6$8]=$0;next}
         END            {for (i in a) print i "|" a}
        '  FS=":|," timebased.log > t1

awk     '/<?xml versio/ {f=1}
         /<\/iSig>/     {f=0; print $0 "\n" }
         f
        ' timebased.log > t2

awk     'FNR==NR        {a[$1]=$2; next}
         FNR==1         {RS="\n\n"}   
                        { for (i in a)
                                {if ($0~i)      {print a $0 > i".log"; close(i".log")
                                                }
                                }
                        }
        ' FS="|"  t1 t2

For loop like what?

Gives what error!

Not sure where you want to slide that in. You can put file after file through one awk script; check the awk internal FILENAME variable to know which file you are processing; or have a statement like FNR==1 {i++} in your script to identify the file number.
You also can change the field separator on the fly: awk 'script' file1 FS="[]:]" file2 file3