how to use variables and gawk in a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to use variables and gawk in a script?
# 1  
Old 05-29-2006
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

Code:
    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... Smilie
# 2  
Old 05-29-2006
Hi, u have to use -v option like u did with NAME var.
# 3  
Old 05-29-2006
but where?

I thought that the -v would act on the rest of the argument ' ' ??
# 4  
Old 05-29-2006
Ex:
Code:
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

# 5  
Old 05-29-2006
... my script

Code:
#!/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

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

# 6  
Old 05-29-2006
Quote:
Originally Posted by pau
Code:
    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}' )

Ok, try

Code:
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}')

# 7  
Old 05-29-2006
Smilie 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 Smilie )
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??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk program not working in a script

I've written a very simple gawk program which runs when I execute it at the POSIX shell but the corresponding '.awk' script I wrote doesn't return any data. I'm on an HP-UX running gawk version 3.1. (w/all the defaults) (As you can probably guess I'm a newbie going through the manual and trying... (2 Replies)
Discussion started by: RUCerius
2 Replies

2. Shell Programming and Scripting

[BASH] Gawk + MYSQL script

Hello! I've got script to write. It should read databases (names, volumes) from table testdatabase and compares it to actually existing databases in /var/lib/mysql/. If there is no informations about database in table - we should see information "There is no declared informations about database... (1 Reply)
Discussion started by: Zimny
1 Replies

3. Shell Programming and Scripting

Help with gawk array, loop in tcsh script

Hi, I'm trying to break a large csv file into smaller files and use unique values for the file names. The shell script i'm using is tcsh and i'm after a gawk one-liner to get the desired outcome. To keep things simple I have the following example with the desired output. fruitlist.csv apples... (6 Replies)
Discussion started by: theflamingmoe
6 Replies

4. Windows & DOS: Issues & Discussions

Extracting variables between commas : GAWK or SED

Hello, I need some help, I got a CSV file called test.txt with this text in it : 08/02/2011;0,677;0,903;1,079;1,336;1,513;1,683 There's only a line and i need to copy theese numbers into variables : 0,677 0,903 1,079 1,336 1,513 1,683 The output file should look like this... (5 Replies)
Discussion started by: jujulips
5 Replies

5. Windows & DOS: Issues & Discussions

Gawk Script in Windows batch file - Help

Good morning all. I have been running into a problem running a simple gawk script that selects every third line from an input file and writes it to an output file. gawk "NR%3==0" FileIn > FileOut I am attempting to run this command from a batch file at the command line. I have several hundred... (6 Replies)
Discussion started by: 10000springs
6 Replies

6. Programming

need help with gawk script

hi i've already created this script. When I execute the script it takes the argument and compares it to the 3rd column of the script. What I was wondering if I could get some help with is. I want to add another column to the script and it will be the result of a set number for example, (2000 - 3rd... (3 Replies)
Discussion started by: gengar
3 Replies

7. Shell Programming and Scripting

Help with gawk script that aggregates ip address ranges

Howdy folks, perhaps someone can help me with this problem. My knowledge of awk is not the best... but I've managed to a certain degree and now I'm stuck. These are the steps and the format outputs, problem is written in red text after the STEP 2: STEP 1 Unformated text file (100+... (3 Replies)
Discussion started by: gustisok
3 Replies

8. Shell Programming and Scripting

Issues with filtering duplicate records using gawk script

Hi All, I have huge trade file with milions of trades.I need to remove duplicate records (e.g I have following records) 30/10/2009,trdeId1,..,.. 26/10/2009.tradeId1,..,..,, 30/10/2009,tradeId2,.. In the above case i need to filter duplicate recods and I should get following output.... (2 Replies)
Discussion started by: nmumbarkar
2 Replies

9. Shell Programming and Scripting

gawk script

Hey guys need your help with an gawk script... here's what I have so far gawk '^d/ {printf "%-20s %-10s %-10s %-10s %-4s%2s %5s\n",$9,$1,$3,$4,$6,$7,$8}' ls.kbr The file ls.kbr is a capture of 'ls-al' What I want gawk to do is: 1) Find only directories (this is working) 2) skip lines... (2 Replies)
Discussion started by: zoo591
2 Replies

10. UNIX for Dummies Questions & Answers

Calculating field using AWK, or GAWK script

Hello all, I'm totally new to UNIX/Linux but I'm taking a course in it at my local JC. My question: I have been tasked with writing a gawk script that will create a nicely formatted report. That part I've done ok on...however, the very last thing that must be done is a calculation of a... (4 Replies)
Discussion started by: Trellot
4 Replies
Login or Register to Ask a Question