awk one liners into a nice script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

awk one liners into a nice script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-12-2013
batka batka is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 18 September 2013, 4:31 AM EDT
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
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:

Code:
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[i]}' 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[i] $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!
Sponsored Links
    #2  
Old 09-12-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 19 September 2014, 8:48 PM EDT
Location: India
Posts: 1,827
Thanks: 25
Thanked 524 Times in 503 Posts
Quote:
Originally Posted by batka View Post
I want to make from these lines a real, nice looking script.
You mean cosmetically good looking ???
Sponsored Links
    #3  
Old 09-12-2013
batka batka is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 18 September 2013, 4:31 AM EDT
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by balajesuri View Post
You mean cosmetically good looking ???
Yes!

A nice script, those 4 lines into a "big" script.
    #4  
Old 09-12-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
As long as we do not have input data and desired output data, its difficult to shorten them.
Sponsored Links
    #5  
Old 09-12-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 September 2014, 7:20 PM EDT
Location: Saskatchewan
Posts: 19,390
Thanks: 790
Thanked 3,270 Times in 3,065 Posts
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

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.
Sponsored Links
    #6  
Old 09-12-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 19 September 2014, 4:45 PM EDT
Location: Aachen, Germany
Posts: 4,273
Thanks: 72
Thanked 1,031 Times in 977 Posts
Do you mean like so (a pretty print, give or take):
Code:
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[i]}
        '  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[i] $0 > i".log"; close(i".log")
                                                }
                                }
                        }
        ' FS="|"  t1 t2

The Following User Says Thank You to RudiC For This Useful Post:
batka (09-18-2013)
Sponsored Links
    #7  
Old 09-18-2013
batka batka is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 18 September 2013, 4:31 AM EDT
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
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?


Code:
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:


Code:
#!/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[i]}
        '  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[i] $0 > i".log"; close(i".log")
                                                }
                                }
                        }
        ' FS="|"  t1 t2

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Set nice value in an executable in a script? scribling Shell Programming and Scripting 3 01-28-2013 06:18 PM
awk - one liners PikK45 Shell Programming and Scripting 6 12-19-2012 12:53 PM
nice command and nice() system call tejbuch Programming 2 11-12-2006 03:21 PM



All times are GMT -4. The time now is 08:05 PM.