how to use variables and gawk in a script?

Hi,

I want to define variables in a shell script and make gawk use them to make some operations

    Mfn = $(grep " 1 " $fitxer | gawk '{print $2}')
    Xfn = $(grep " 1 " $fitxer | gawk '{print $3}')
    Yfn = $(grep " 1 " $fitxer | gawk '{print $4}')
    Zfn = $(grep " 1 " $fitxer | gawk '{print $5}')

# Calculem l'eix semimajor del sistema 1-particula

   cat $fitxer | gawk -v NAME=$num_part '
         /AS/ {TIME=$4}
         $1==NAME {print TIME,(sqrt(($3-$Xfn)^2+($4-$Yfn)^2+($5-$Zfn))*$Mfn/(2*$Mfn-$11*($6^2+$7^2+$8^2))); exit}
    '
done

But gawk doesn't understand that $Xfn, $Yfn, $Zfn and $Mfn are not fields but values defined before

How to do this??

I don't have any idea of perl or python, so I hav to go through shell... :o

Hi, u have to use -v option like u did with NAME var.

but where?

I thought that the -v would act on the rest of the argument ' ' ??

Ex:

cat $fitxer | gawk -v XFN=$Xfn -v ZFN=$Zfn -v MFN=$Mfn -v YFN=$Yfn -v NAME=$num_part '
         /AS/ {TIME=$4}
         $1==NAME {print TIME,(sqrt(($3-XFN)^2+($4-YFN)^2+($5-ZFN))*MFN/(2*MFN-$11*($6^2+$7^2+$8^2))); exit}
    '
done

... my script

#!/usr/bin/env zsh

LANG=C LC_ALL=C
export LANG LC_ALL

num_part=$1 # parametre en la linia de comandament, particula a cercar

print "\#Eix semimajor i excentricitat de la particula $num_part"
print "\#Col1:t  Col2:a  Col3:e..ncara no fet"

for fitxer in $( ls Splotch*_part.asc ); do;

# Cerquem les dades del fn, M, X, Y i Z

    Mfn = $( grep " 1 " $fitxer | gawk '{print $2}' )
    Xfn = $( grep " 1 " $fitxer | gawk '{print $3}' )
    Yfn = $( grep " 1 " $fitxer | gawk '{print $4}' )
    Zfn = $( grep " 1 " $fitxer | gawk '{print $5}' )

# Calculem l'eix semimajor del sistema 1-particula

cat $fitxer | gawk -v XFN=$Xfn -v YFN=$Yfn -v ZFN=$Zfn -v MFN=$Mfn -v NAME=$num_part '
         /AS/ {TIME=$4}
         $1==NAME {print TIME,(sqrt(($3-XFN)^2+($4-YFN)^2+($5-ZFN))*MFN/(2*MFN-$11*($6^2+$7^2+$8^2))); exit}
    '
done

... the result

elachistos| ./Semi_i_Excentr.sh 2                                                                          
#Eix semimajor i excentricitat de la particula 2
#Col1:t  Col2:a  Col3:e..ncara no fet
./Semi_i_Excentr.sh:15: command not found: Mfn
./Semi_i_Excentr.sh:16: command not found: Xfn
./Semi_i_Excentr.sh:17: command not found: Yfn
./Semi_i_Excentr.sh:18: command not found: Zfn
0.00000E+00 0
./Semi_i_Excentr.sh:15: command not found: Mfn
./Semi_i_Excentr.sh:16: command not found: Xfn
./Semi_i_Excentr.sh:17: command not found: Yfn
./Semi_i_Excentr.sh:18: command not found: Zfn
0.10000E+01 0
./Semi_i_Excentr.sh:15: command not found: Mfn
./Semi_i_Excentr.sh:16: command not found: Xfn
./Semi_i_Excentr.sh:17: command not found: Yfn
./Semi_i_Excentr.sh:18: command not found: Zfn

Ok, try

Mfn=$(grep " 1 " $fitxer | gawk '{print $2}')
Xfn=$(grep " 1 " $fitxer | gawk '{print $3}')
Yfn=$(grep " 1 " $fitxer | gawk '{print $4}')
Zfn=$(grep " 1 " $fitxer | gawk '{print $5}')

:eek: gosh! that was it

thanks a lot

BTW what do you recommend me to learn? perl or python or ruby? I am a bit fed up that this scripting is so strict... I feel like programming in fortran (what I also do :stuck_out_tongue: )
Usually I have to do this kind of operations and sometimes the data blocks are up to 128000 lines with eight or more columns... and I have usually about 2500 data files like that... in total sometimes about 6GB/2500 files

So what'd be faster and easier to apply/learn, python, perl, ruby??

http://www.bsdforums.org/forums/archive/index.php/t-10459.html
maybe good for u..

I think the answer is python...

indeed...

now I need built-in functions, like max, min

How to select out of a list the maximum value with gawk?

This is the list

Dist=$(echo "sqrt(($Xpart-$Xfn)^2+($Ypart-$Yfn)^2+($Zpart-$Zfn)^2)" | bc)