public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] Help with bash/awk script
@ 2006-08-23 14:30 Ow Mun Heng
  2006-08-23 15:17 ` Etaoin Shrdlu
  2006-08-23 15:20 ` Bo Ørsted Andresen
  0 siblings, 2 replies; 5+ messages in thread
From: Ow Mun Heng @ 2006-08-23 14:30 UTC (permalink / raw
  To: gentoo-user

I'm trying to figure out how to do this sequence in this bash script.
The problem I'm having is how to make $i to change according to the
changes in $x

My current solution is a bit of a hack and stupid.

One more thing, my current solution will parse the file _each_ time for
_each_value/head which essentially means it reads the file multiple
times. Is there a better way so to reduce the # of reads/parses? (Does
awk parse the file once or multiple times, that is if I were to rewrite
the below entirely in awk language)

Thanks


#!/bin/bash

filename=$1
heads=6
tpi=7
x=0
y=1
z=1
clear
while  (( $x < $heads ))
  do
    finaltpi=`cat $1 | awk  "/TPI chosen/" | awk -v pat="$x" '{if
(NR==pat+1) print $5}'`
  while (( $y <= $tpi ))
          do
                if [ "$x" -eq 0 ]
            then
                  for i in `seq 1 7` <-----
                    do
                     tpiert=`egrep -i "(average)" $1 | awk -v pat="$i"
'{ if(NR==pat) print $5","$10}'`
                         echo $filename,$x,$y,$finaltpi,$tpiert
                        ((y++))
                    done
                        
                elif [ "$x" -eq 1 ]
            then
                  for i in `seq 8 14` <-----
                    do
                     tpiert=`egrep -i "(average)"  $1| awk -v pat="$i"
'{ if(NR==pat) print $5","$10}'`
                         echo $filename,$x,$y,$finaltpi,$tpiert
                        ((y++))
                    done
                elif [ "$x" -eq 2 ]
            then
                  for i in `seq 15 21` <-----
                    do
                     tpiert=`egrep -i "(average)"  $1 | awk -v pat="$i"
'{ if(NR==pat) print $5","$10}'`
                         echo $filename,$x,$y,$finaltpi,$tpiert
                        ((y++))
                    done
                elif [ "$x" -eq 3 ]
            then
                  for i in `seq 22 28` <-----
                    do
                     tpiert=`egrep -i "(average)"  $1 | awk -v pat="$i"
'{ if(NR==pat) print $5","$10}'`
                         echo $filename,$x,$y,$finaltpi,$tpiert
                        ((y++))
                    done
       elif [ "$x" -eq 4 ]
            then
                  for i in `seq 29 35` <-----
                    do
                     tpiert=`egrep -i "(average)"  $1 | awk -v pat="$i"
'{ if(NR==pat) print $5","$10}'`
                         echo $filename,$x,$y,$finaltpi,$tpiert
                        ((y++))
                    done
           elif [ "$x" -eq 5 ]
            then
                  for i in `seq 36 42` <-----
                    do
                     tpiert=`egrep -i "(average)"  $1 | awk -v pat="$i"
'{ if(NR==pat) print $5","$10}'`
                         echo $filename,$x,$y,$finaltpi,$tpiert
                        ((y++))
                    done
                fi              
          done
        let "y=1"
    ((x++))
  done


-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [gentoo-user] Help with bash/awk script
  2006-08-23 14:30 [gentoo-user] Help with bash/awk script Ow Mun Heng
@ 2006-08-23 15:17 ` Etaoin Shrdlu
  2006-08-23 16:07   ` Ow Mun Heng
  2006-08-23 15:20 ` Bo Ørsted Andresen
  1 sibling, 1 reply; 5+ messages in thread
From: Etaoin Shrdlu @ 2006-08-23 15:17 UTC (permalink / raw
  To: gentoo-user

On Wednesday 23 August 2006 16:30, Ow Mun Heng wrote:

> if [ "$x" -eq 0 ]
>            then
>    for i in `seq 1 7` <-----
>                     do
>                      tpiert=`egrep -i "(average)" $1 | awk -v pat="$i"
> '{ if(NR==pat) print $5","$10}'`
>                          echo $filename,$x,$y,$finaltpi,$tpiert
>                         ((y++))
>                     done
>                        
>                 elif [ "$x" -eq 1 ]
>             then
>                   for i in `seq 8 14` <-----
>                     do
>                      tpiert=`egrep -i "(average)"  $1| awk -v pat="$i"
> '{ if(NR==pat) print $5","$10}'`
>                          echo $filename,$x,$y,$finaltpi,$tpiert
>                         ((y++))
>                     done
>                 elif [ "$x" -eq 2 ]

If I understand correctly, you want to avoid all the if/elif tests on $x.

What about

low=`expr $x \* 7 + 1`
high=`expr $low + 6`

for i in `seq $low $high` ; do

#  your code here

done

-- 
gentoo-user@gentoo.org mailing list


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [gentoo-user] Help with bash/awk script
  2006-08-23 14:30 [gentoo-user] Help with bash/awk script Ow Mun Heng
  2006-08-23 15:17 ` Etaoin Shrdlu
@ 2006-08-23 15:20 ` Bo Ørsted Andresen
  2006-08-23 16:24   ` Ow Mun Heng
  1 sibling, 1 reply; 5+ messages in thread
From: Bo Ørsted Andresen @ 2006-08-23 15:20 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 896 bytes --]

On Wednesday 23 August 2006 16:30, Ow Mun Heng wrote:
> (Does awk parse the file once or multiple times, that is if I were to
> rewrite the below entirely in awk language)

awk parses the file only once. One line at a time. Of course that goes for
every invocation of awk... ;)

>     finaltpi=`cat $1 | awk  "/TPI chosen/" | awk -v pat="$x" '{if
> (NR==pat+1) print $5}'`

finaltpi=`awk -v pat="$x" '/TPI chosen/{if (NR==pat+1) print $5' < $x`

>                      tpiert=`egrep -i "(average)" $1 | awk -v pat="$i"
> '{ if(NR==pat) print $5","$10}'`

tpiert=`awk -v pat="$i" 'tolower($0)~/(average)/{ if(NR==pat) print $5","$10}' < $1`

If you want more help with this I would suggest that you posted a short sample
input file and a sample of the output you want to achieve. It does make it a
heck of a lot easier to grasp what it is you want to do...

-- 
Bo Andresen

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [gentoo-user] Help with bash/awk script
  2006-08-23 15:17 ` Etaoin Shrdlu
@ 2006-08-23 16:07   ` Ow Mun Heng
  0 siblings, 0 replies; 5+ messages in thread
From: Ow Mun Heng @ 2006-08-23 16:07 UTC (permalink / raw
  To: gentoo-user

On Wed, 2006-08-23 at 17:17 +0200, Etaoin Shrdlu wrote:
> On Wednesday 23 August 2006 16:30, Ow Mun Heng wrote:
> 
> > if [ "$x" -eq 0 ]
> >            then
> >    for i in `seq 1 7` <-----
> >                     do
> >                      tpiert=`egrep -i "(average)" $1 | awk -v pat="$i"
> > '{ if(NR==pat) print $5","$10}'`
> >                          echo $filename,$x,$y,$finaltpi,$tpiert
> >                         ((y++))
> >                     done
> >                        
> >                 elif [ "$x" -eq 1 ]
> >             then
> >                   for i in `seq 8 14` <-----
> >                     do
> >                      tpiert=`egrep -i "(average)"  $1| awk -v pat="$i"
> > '{ if(NR==pat) print $5","$10}'`
> >                          echo $filename,$x,$y,$finaltpi,$tpiert
> >                         ((y++))
> >                     done
> >                 elif [ "$x" -eq 2 ]
> 
> If I understand correctly, you want to avoid all the if/elif tests on $x.
> 
> What about
> 
> low=`expr $x \* 7 + 1`
> high=`expr $low + 6`
> 
> for i in `seq $low $high` ; do
> 
> #  your code here
> 

This works _Great_!!

Thanks. I was thinking along the same lines, but somehow can't get it
right.

-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [gentoo-user] Help with bash/awk script
  2006-08-23 15:20 ` Bo Ørsted Andresen
@ 2006-08-23 16:24   ` Ow Mun Heng
  0 siblings, 0 replies; 5+ messages in thread
From: Ow Mun Heng @ 2006-08-23 16:24 UTC (permalink / raw
  To: gentoo-user

On Wed, 2006-08-23 at 17:20 +0200, Bo Ørsted Andresen wrote:
> On Wednesday 23 August 2006 16:30, Ow Mun Heng wrote:
> > (Does awk parse the file once or multiple times, that is if I were to
> > rewrite the below entirely in awk language)
> 
> awk parses the file only once. One line at a time. Of course that goes for
> every invocation of awk... ;)
> 
> >     finaltpi=`cat $1 | awk  "/TPI chosen/" | awk -v pat="$x" '{if
> > (NR==pat+1) print $5}'`
> 
> finaltpi=`awk -v pat="$x" '/TPI chosen/{if (NR==pat+1) print $5' < $x`
> 
> >                      tpiert=`egrep -i "(average)" $1 | awk -v pat="$i"
> > '{ if(NR==pat) print $5","$10}'`
> 
> tpiert=`awk -v pat="$i" 'tolower($0)~/(average)/{ if(NR==pat) print $5","$10}' < $1`
> 
> If you want more help with this I would suggest that you posted a short sample
> input file and a sample of the output you want to achieve. It does make it a
> heck of a lot easier to grasp what it is you want to do...

I've managed to solve it using Etaoin's help but I'll post the input and
output files here anyway, (as I can't understand why you're using $x in
the 1st query. Ah.. Figured it out, you meant to use $1 (to subtitute
the cat $1. Is there a performance difference?

I can't use the 2nd invocation of awk (for tpiert) mainly because the NR
is different

$awk 'tolower($0)~/(average)/{ print NR,$5","$10}' < test     
4 1.4E+005,1.0E+000
6 1.9E+004,9.0E+001

it doesn't line up. the grep makes it this way

$egrep -i "(average)" test | awk '{print NR,$5","$10}'
1 1.4E+005,1.0E+000
2 1.9E+004,9.0E+001

HEnce I can use the NR sequentially.

Thanks

$cat test 
TPI chosen for x[0]: 7
TPI chosen for x[1]: 6

-------------------------------------------------------
                         =====> average before = 1.4E+005 ,   average
after = 1.0E+000 

-------------------------------------------------------
                         =====> average before = 1.9E+004 ,   average
after = 9.0E+001 

-------------------------------------------------------

$cat output
test,0,1,7,1.4E+005,1.0E+000
test,0,2,7,1.9E+004,9.0E+001


-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-08-23 16:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-23 14:30 [gentoo-user] Help with bash/awk script Ow Mun Heng
2006-08-23 15:17 ` Etaoin Shrdlu
2006-08-23 16:07   ` Ow Mun Heng
2006-08-23 15:20 ` Bo Ørsted Andresen
2006-08-23 16:24   ` Ow Mun Heng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox