awk output to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk output to variables
# 1  
Old 09-14-2010
awk output to variables

Im trying to read the awk output to two variables. In the below code i need to store the if part output to a variable (NEW) and else part output to another variable (BAD). Pls shed some light on this snippet or if there is any other way to achieve it wud be appreciated (only condition is it needs to be done using awk)

FYI..The math part in the code (d= ...) does the percnt diffrence.

Code:
echo 10 30 |awk -v T=67 '{d=(($1-$2)/$2)*100} {if (d > T || d < -T) print 1,T read NEW; else print 0,T read BAD}'

# 2  
Old 09-14-2010
Not 100% sure to understand your requirements are but that might answer them:
Code:
eval $(echo 10 30 | awk -v T=67 ' { d=(($1-$2)/$2)*100 } { if (d > T || d < -T) printf("NEW='"'%d %d'"'\n",1,T) ; else printf("BAD='"'%d %d'"'\n",0,T) }')
echo NEW=$NEW
echo BAD=$BAD

# 3  
Old 09-15-2010
Thanks much.It worked out. Is it possible to store the output in two variables in the same printf syntax? i.e Need to have NEW=1 and say NEW_2=T (the value of T) in the below statement.
Code:
printf("NEW='"'%d %d'"'\n",1,T)



---------- Post updated at 02:21 PM ---------- Previous update was at 02:16 PM ----------

I tried as below, but the NEW_2 has null value in it
Code:
eval $(echo 10 30 | awk -v T=38 '{ d=(($1-$2)/$2)*100 } { if (d > T || d < -T) printf("NEW='"'%d'"'\n",1);printf("BAD='"'%d'"'\n",0);printf("NEW_2='"'%d'"'\n",T)}')

# 4  
Old 09-15-2010
Code:
eval $(echo 10 30 | awk -v T=38 ' { d=(($1-$2)/$2)*100 } { if (d > T || d < -T) printf("NEW=1\n") ; else printf("BAD=0\n") ; printf("T=%d\n",T) }')
echo NEW=$NEW
echo BAD=$BAD
echo T=$T

# 5  
Old 09-15-2010
Computer

Ah great!! And sorry for bugging you again but i have the last questions, pls be kind to answer.

Is it possible to assign the output of below to 2 diff variables in the single printf statement itself?. I mean the value 1 and T to two variables..Jus to know if this is possible!

Code:
printf("NEW='"'%d %d'"'\n",1,T)

# 6  
Old 09-15-2010
Sure:
Code:
printf("VAR1=%d;VAR2=%d\n",1,T)

Note that passing a constant is suboptimal. This can be simplified that way:
Code:
printf("VAR1=1;VAR2=%d\n",T)

# 7  
Old 09-15-2010
MySQL

Thanks a bunch. If im not wrong now, i cud guess the usage of printf statement in awk is similar to C programming.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store output in variables instead of files

Hi All, I have written a (bash) function which generates multiple files say file1 file2 file3 now I want to reduce the generation of these three files i.e. store the output of three files in variables, and generate the same results, in-order to avoid multiple creation of files how is that... (7 Replies)
Discussion started by: sam@sam
7 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

4. Shell Programming and Scripting

How to redirect the output of awk to different variables?

Hi, I search for a string "can be any" and print the fields $4,$6 in to variables. $ cat input <value ='can be any' string='many' version='123'/> <value ='xyz' string='less' version='456'/> $ cat tried_code SRG=`awk -F\' '{ print $4 }' input` VER=`awk -F\' '{ print $6 }' input` ... (4 Replies)
Discussion started by: greet_sed
4 Replies

5. 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

6. Shell Programming and Scripting

awk output to multiple variables

Hi I need to assign the ouput of a awk statement to two variables; below is a example of the txt file i have which I use awk against sample file testval,USA,loc2,testing02 testval1,GB,loc4,testing01 awk statement awk -F , '{print $2,$3}' USA loc2 GB loc4 I need a method where... (6 Replies)
Discussion started by: duckeggs01
6 Replies

7. Shell Programming and Scripting

Want to parse output for variables in Bash

Trying to finish up my script that automates some video encoding work. Situation: There is an MKV file to be transcoded. Problem: MKVINFO will give a bunch of output about an MKV file, included in that output are two lines I am interested in: | + Default duration: 41.708ms (23.976 fps... (9 Replies)
Discussion started by: randyharris
9 Replies

8. Shell Programming and Scripting

Storing awk output into variables

Hi all, Currently, i have a log file seperated by 'tab' and each record starting with a new line. i managed to retrieve the column that i'm interested in. (source_ip_address: xxx.xxx.xxx.xxx). example of awk output: '{ print $43 }' assuming the field is at column 43. 10.10.10.10... (4 Replies)
Discussion started by: faelric
4 Replies

9. UNIX for Dummies Questions & Answers

output ls in variables

Hi all, I need some help for a command in a script with bash. I need the output from ls in separate variables to compare them later with other files. Example: ls map1 gives: a.txt photo.bmp log.out gcd.sh I would like to have that: a1=a.txt a2=photo.bmp ... Thanks for the... (1 Reply)
Discussion started by: Sciencestudent
1 Replies

10. Shell Programming and Scripting

Assigning nawk output to variables

I do a lot of command line scripting to capture data from files or other command output. I've checked in a number of Unix and scripting books but for the life of me I can't find out how to asign field data from nawk output into variables that I can manipulate later. For example, reading a two... (6 Replies)
Discussion started by: steveje0711
6 Replies
Login or Register to Ask a Question