Visit Our UNIX and Linux User Community


awk one liners into a nice script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk one liners into a nice script
# 1  
Old 09-12-2013
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!
# 2  
Old 09-12-2013
Quote:
Originally Posted by batka
I want to make from these lines a real, nice looking script.
You mean cosmetically good looking ???
# 3  
Old 09-12-2013
Quote:
Originally Posted by balajesuri
You mean cosmetically good looking ???
Yes! Smilie

A nice script, those 4 lines into a "big" script.
# 4  
Old 09-12-2013
As long as we do not have input data and desired output data, its difficult to shorten them.
# 5  
Old 09-12-2013
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 Smilie

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.
# 6  
Old 09-12-2013
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

This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-18-2013
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


Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. AIX

Useful and nice script for AIX,but outdated

How can I quickly see disk usage for my entire system? - IBM: AIX FAQ - Tek-Tips I tried it on aix7.1with ./script -dand give me VG PV size used free location Description rootvg 128 397 149 rootvg 128 212 334 ... (1 Reply)
Discussion started by: Linusolaradm1
1 Replies

2. What is on Your Mind?

Those simple one liners

I wanted to say LOL and punch my face when I saw post#11 (where Don_Cragun even reduced the string manipulation with a simple regex) in the thread https://www.unix.com/shell-programming-scripting/220553-add-0-start-filename-2.html I mean, when things can be done with just a one liner, sometimes I... (6 Replies)
Discussion started by: ahamed101
6 Replies

3. Shell Programming and Scripting

Set nice value in an executable in a script?

Is it possible to set a nice value for an executable in a script so that every time the executable runs it has this nice value? I'm trying to set aerender (After Effects terminal renderer) to run at +18 by replacing the original aerender script with a bash script with something like this in it: ... (3 Replies)
Discussion started by: scribling
3 Replies

4. Shell Programming and Scripting

awk - one liners

Guys, I have a requirement like this. A file has >5K records always. Separated by "|", it has 30 fields for each line. In some lines, I am getting an odd field. say, the 15th field is supposed to be 2 characters but comes in as >2. In this case, for resolving this I need to copy the value of... (6 Replies)
Discussion started by: PikK45
6 Replies

5. Red Hat

KSH script help needed ( nice error trap routine ?)

I am running a script that runs a loop and executes a command on ${i} until the end of the for loop. From time to time the command generates an error ( which is good) for example ERROR0005: How can I trap the error and send an email echoing the ${i} variable in the loop and the error ? ... (2 Replies)
Discussion started by: pcpinkerton
2 Replies

6. UNIX for Dummies Questions & Answers

How to Turn perl one-liners into full perl script?

I have the following command prompt perl one liner: perl -e 's/\(\)\\,\"]//g; s/^\s//g; s/;/\n/g' -pi result1 I tried to move this one liner into a perl script I am practicing with (just started learning perl right now). I tried the following (I only know how to open a file and output to a... (1 Reply)
Discussion started by: EDALBNUG
1 Replies

7. Programming

nice command and nice() system call

Hi I want to implement the nice command in the shell that I am building. I came to know that there is a corresponding nice() system call for the same. But since I will be forking different processes to run different commands typed on the command prompt, is there any way I can make a command... (2 Replies)
Discussion started by: tejbuch
2 Replies

Featured Tech Videos