Passing value in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Passing value in awk
# 1  
Old 07-11-2014
Passing value in awk

Hi,

I want to pass value of the variable i which is the radius in the following command in bash script:
Code:
awk '{r=sqrt($2^2+$3^2+$4^2); if(r<=$i) print r }' input.txt

It ignores $i in if command and print the whole range. I tried to replace $i with 6 and it worked. Would some one help me please? Thank you.

Last edited by Franklin52; 07-12-2014 at 03:48 PM.. Reason: Please use code tags
# 2  
Old 07-11-2014
Use awk -v option for variable assignment and use that variable instead:
Code:
awk -v I="$i" '{r=sqrt($2^2+$3^2+$4^2); if(r<=I) print r }' input.txt

This User Gave Thanks to Yoda For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Passing variables into AWK

I'm trying to use awk to write new entries to a hosts file if they don't exist. I need to do so depending on the type of system I have. Below is what I have, but it isn't working. awk -v myip1=$IP1 myip2=$IP2 myhost1=$HOST1 myhost2=$HOST2' BEGIN { mqhost1=0; mqhost2=0; stap1=0; stap2=0; } ... (4 Replies)
Discussion started by: Boomn4x4
4 Replies

4. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

5. Shell Programming and Scripting

Passing a parameter to AWK

Hi All, I am trying to pass a parameter to AWK on my KSH shell prompt as below. var1=2 echo $var1 awk -v var2=${var1} '{print var2}' testfile.txt I am passing the input file (testfile) to awk to get some o/p. It is having 10 records. When I run AWK, it is throwing the following errors... (1 Reply)
Discussion started by: Raamc
1 Replies

6. Shell Programming and Scripting

Passing two files to awk

I want to pass the first file as input argument using -v then first get awk to go through mref then pert.txt awk -v mref="reference.txt" -f ../../A-Scripts/convert-zv-zp.awk pert.txt BEGIN { Version = "V04" if ( !mref ) { usage(Version) print "ERROR: mref not... (1 Reply)
Discussion started by: kristinu
1 Replies

7. Shell Programming and Scripting

Passing arguments to awk

I have an awk script below which I call using for example awk -f ../../A-Scripts/select-model.awk iterations.txt 16x12 10 I want to be able to use it in a different way like this awk -f ../../A-Scripts/select-model.awk iterations.txt nxz=16x12 iter=10 or awk -f... (1 Reply)
Discussion started by: kristinu
1 Replies

8. Shell Programming and Scripting

Passing non Awk Argument in Awk

Dear Conerned, I am facing a situation where i need to pass an argument which is non-awk variable like day=090319 awk '/TID:R/ && /TTIN:/' transaction.log I want to add this day variable like below awk '/TID:R$day/ && /TTIN:/' transaction.log But it is not working. :confused: (1 Reply)
Discussion started by: saifurshaon
1 Replies

9. Shell Programming and Scripting

Passing Value from awk to shell

I'm writting a script to calculate the total files and number of files that have been generated but not able to access the value of qwk variable in Shell. Please suggest.. Script :: cd /home/singhraa/tmp/scripts count=0 total=`wc -l hk_jobs.txt` #total number of files echo $total ... (3 Replies)
Discussion started by: raman1605
3 Replies

10. Shell Programming and Scripting

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies
Login or Register to Ask a Question