For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2008 > AWK Optimization









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author AWK Optimization
dnlchen@gmail.com

2008-02-27, 6:58 pm

Input (MySQL status):

| Com_select | 16653146213 |
| Com_delete | 43654772 |
| Com_insert | 637410971 |
| Com_update | 673829655 |

Output:
Read Query #
Write Query #

Below script works, but I think there must be a concise AWK one.

#!/bin/sh

declare -i m n i j

m=`awk '$2 == "Com_select" {print $4}' /var/log/mysql/status1`
n=`awk '$2 == "Com_select" {print $4}' /var/log/mysql/status2`

i=`awk '$2 == "Com_delete" || $2 == "Com_insert" || $2 ==
"Com_update" {sum += $4} END {print sum}' /var/log/mysql/status1`
j=`awk '$2 == "Com_delete" || $2 == "Com_insert" || $2 ==
"Com_update" {sum += $4} END {print sum}' /var/log/mysql/status2`

awk -v Read1=$m -v Read2=$n 'BEGIN { printf( "%.4f\n", (Read2 - Read1)/
300 ) }'
awk -v Write1=$i -v Write2=$j 'BEGIN { printf( "%.4f\n", (Write2 -
Write1)/300 ) }'
Janis

2008-02-28, 4:00 am

On 28 Feb., 01:44, dnlc...@gmail.com wrote:
> Input (MySQL status):
>
> | Com_select =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | 16653146213 =A0 |
> | Com_delete =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | 43654772 =A0 =A0 =A0 =A0|
> | Com_insert =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| 637410971 =A0 =A0 =A0|
> | Com_update =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| 673829655 =A0 =A0 =A0|
>
> Output:
> Read Query #
> Write Query #
>
> Below script works, but I think there must be a concise AWK one.


Yes. Do everything in one awk script. The key is to read both
files and distinguish your operations depending on which file
you are perocessing. The frame is...

awk '
NR=3D=3DFNR {
# do the things to be done for file "status1"
next;
}
NR!=3DFNR {
# do the things to be done for file "status2"
next;
}
' var/log/mysql/status1 var/log/mysql/status2

You will extend the above conditions "NR <op> FNR" by the
conditions you have in your various awk calls, and you will
use separate variables to sum up things. The read/write
calculations will be done in the END section.

Janis

>
> #!/bin/sh
>
> declare -i m n i j
>
> m=3D`awk '$2 =3D=3D "Com_select" {print $4}' /var/log/mysql/status1`
> n=3D`awk '$2 =3D=3D "Com_select" {print $4}' /var/log/mysql/status2`
>
> i=3D`awk '$2 =3D=3D "Com_delete" || $2 =3D=3D "Com_insert" || $2 =3D=3D
> "Com_update" {sum +=3D $4} END {print sum}' /var/log/mysql/status1`
> j=3D`awk '$2 =3D=3D "Com_delete" || $2 =3D=3D "Com_insert" || $2 =3D=3D
> "Com_update" {sum +=3D $4} END {print sum}' /var/log/mysql/status2`
>
> awk -v Read1=3D$m -v Read2=3D$n 'BEGIN { printf( "%.4f\n", (Read2 - Read1)=

/
> 300 ) }'
> awk -v Write1=3D$i -v Write2=3D$j 'BEGIN { printf( "%.4f\n", (Write2 -
> Write1)/300 ) }'


Ed Morton

2008-02-28, 7:58 am



On 2/27/2008 6:44 PM, dnlchen@gmail.com wrote:
> Input (MySQL status):
>
> | Com_select | 16653146213 |
> | Com_delete | 43654772 |
> | Com_insert | 637410971 |
> | Com_update | 673829655 |
>
> Output:
> Read Query #
> Write Query #
>
> Below script works, but I think there must be a concise AWK one.
>
> #!/bin/sh
>
> declare -i m n i j
>
> m=`awk '$2 == "Com_select" {print $4}' /var/log/mysql/status1`
> n=`awk '$2 == "Com_select" {print $4}' /var/log/mysql/status2`
>
> i=`awk '$2 == "Com_delete" || $2 == "Com_insert" || $2 ==
> "Com_update" {sum += $4} END {print sum}' /var/log/mysql/status1`
> j=`awk '$2 == "Com_delete" || $2 == "Com_insert" || $2 ==
> "Com_update" {sum += $4} END {print sum}' /var/log/mysql/status2`
>
> awk -v Read1=$m -v Read2=$n 'BEGIN { printf( "%.4f\n", (Read2 - Read1)/
> 300 ) }'
> awk -v Write1=$i -v Write2=$j 'BEGIN { printf( "%.4f\n", (Write2 -
> Write1)/300 ) }'


Something like this (untested):

awk '
$2=="Com_select" {read[NR==FNR] = $4}
$2~/^Com_(delete|insert|update)$/ {write[NR==FNR] += $4}
END {printf "%.4f\n%.f4\n", (read[0] - read[1])/300, (write[0] - write[1])/300}
' /var/log/mysql/status1 /var/log/mysql/status2

If your posted input is truly all you have in the files (i.e. $2 is always one
of the Com_* you show), then all you really need is:

awk '$2=="Com_select" {read[NR==FNR] = $4; next} {write[NR==FNR] += $4}
END {printf "%.4f\n%.f4\n", (read[0] - read[1])/300, (write[0] - write[1])/300}
' /var/log/mysql/status1 /var/log/mysql/status2

Ed.

dnlchen@gmail.com

2008-02-28, 9:59 pm

On Feb 28, 4:43=A0am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/27/2008 6:44 PM, dnlc...@gmail.com wrote:
>
>
>
>
>
>
[color=darkred]
[color=darkred]
>
>
>
>
>
>
>
1)/[color=darkred]
>
> Something like this (untested):
>
> awk '
> $2=3D=3D"Com_select" {read[NR=3D=3DFNR] =3D $4}
> $2~/^Com_(delete|insert|update)$/ {write[NR=3D=3DFNR] +=3D $4}
> END {printf "%.4f\n%.f4\n", (read[0] - read[1])/300, (write[0] - write[1])=

/300}
> ' /var/log/mysql/status1 /var/log/mysql/status2
>

this will ouput:
1030.0533
1944

my script's output:
1030.0533
193.7133

I can't tell which is correct;)

> If your posted input is truly all you have in the files (i.e. $2 is always=

one
> of the Com_* you show), then all you really need is:
>
> awk '$2=3D=3D"Com_select" {read[NR=3D=3DFNR] =3D $4; next} {write[NR=3D=3D=

FNR] +=3D $4}
> END {printf "%.4f\n%.f4\n", (read[0] - read[1])/300, (write[0] - write[1])=

/300}
> ' /var/log/mysql/status1 /var/log/mysql/status2
>
> =A0 =A0 =A0 =A0 Ed.- Hide quoted text -
>
> - Show quoted text -


dnlchen@gmail.com

2008-02-28, 9:59 pm

On Feb 28, 4:43=A0am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/27/2008 6:44 PM, dnlc...@gmail.com wrote:
>
>
>
>
>
>
[color=darkred]
[color=darkred]
>
>
>
>
>
>
>
1)/[color=darkred]
>
> Something like this (untested):
>
> awk '
> $2=3D=3D"Com_select" {read[NR=3D=3DFNR] =3D $4}
> $2~/^Com_(delete|insert|update)$/ {write[NR=3D=3DFNR] +=3D $4}
> END {printf "%.4f\n%.f4\n", (read[0] - read[1])/300, (write[0] - write[1])=

/300}
> ' /var/log/mysql/status1 /var/log/mysql/status2
>


oh, I got it.
printf "%.4f\n%.f4\n", =3D=3D> printf "%.4f\n%.4f\n",

> If your posted input is truly all you have in the files (i.e. $2 is always=

one
> of the Com_* you show), then all you really need is:
>
> awk '$2=3D=3D"Com_select" {read[NR=3D=3DFNR] =3D $4; next} {write[NR=3D=3D=

FNR] +=3D $4}
> END {printf "%.4f\n%.f4\n", (read[0] - read[1])/300, (write[0] - write[1])=

/300}
> ' /var/log/mysql/status1 /var/log/mysql/status2
>
> =A0 =A0 =A0 =A0 Ed.- Hide quoted text -
>
> - Show quoted text -


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com