Passing values from awk to shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing values from awk to shell variable
# 1  
Old 02-27-2013
Passing values from awk to shell variable

I am writing a script where I need awk to test if two columns are the same and shell to do something if they are or are not.

Here is the code I'm working with:
Code:
@ test = 0
...
test = `awk '{if($1!=$2) print 1; else print 0}' time_test.tmp`
#time_test.tmp holds two values separated by a space
echo $test


For some reason, when I run this script it never seems to get to the echo. Is there some other way to pass a value to a script variable? I need test to be 1 if the two values in time_test.tmp are not the same, and 0 if they are.

Thank you.

Last edited by Scott; 02-27-2013 at 03:43 PM.. Reason: Please use code tags
# 2  
Old 02-27-2013
Code:
test=`awk '{if($1!=$2) print 1; else print 0}' time_test.tmp`

echo $test

# 3  
Old 02-27-2013
I think the space around the = creates problems.
Code:
test=`awk '{if($1!=$2) print 1; else print 0}' time_test.tmp`

or better, do not use backtics
Code:
test=$(awk '{if($1!=$2) print 1; else print 0}' time_test.tmp)

some variation
Code:
test=$(awk '{print ($1!=$2)?1:0}' time_test.tmp)


Last edited by Jotne; 02-27-2013 at 03:49 PM..
# 4  
Old 02-27-2013
Got it working! I knew it was something like that... Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign awk values to shell variable?

Hi Gurus, I have a script which assign awk output to shell variable. current it uses two awk command to assign value to two variables. I want to use one command to assign two values to two variables. I tried the code, but it does't work. kindly provide your suggestion. current code... (2 Replies)
Discussion started by: green_k
2 Replies

2. UNIX for Dummies Questions & Answers

Passing Shell Variable to awk

Hello All, May i please why my shell variable is not getting passed into awk script. #!/bin/bash -vx i="1EB07C50" /bin/awk -v ID="$i" '/ID/ {match($0,/ID/);print substr($0,RSTART,RLENGTH)}' /var/log/ScriptLogs/keys.13556.txt Thank you. (1 Reply)
Discussion started by: Ariean
1 Replies

3. Shell Programming and Scripting

Passing multiple column values to UNIX variable

sqlplus -s $USER_ID@$SID/$PWD<<EOF>sql_1.txt set feedback off set heading off select 114032 as c_1 from dual ; EOF for i in `cat sql_1.txt` do sh script_1.sh $i Currently i am passing one column value to the single unix variable. How can i pass the values from 2... (2 Replies)
Discussion started by: rafa_fed2
2 Replies

4. Shell Programming and Scripting

Passing shell variable to awk script

I want to pass a shell variable to awk script : # cat file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO 92000 4087.5625 4 96 #... (8 Replies)
Discussion started by: Reboot
8 Replies

5. UNIX for Dummies Questions & Answers

Passing of variable values to remote server

Hi, My script will take 3 i/p's from user. Now i need to pass these 3 values to remote server. Please find my code. while do echo " To which server you want to connect ? " echo " 1. server1 \n" echo " 2. server2 \n" read opt_server if then echo "enter the... (2 Replies)
Discussion started by: sree143reddy
2 Replies

6. UNIX for Dummies Questions & Answers

Passing a Shell Variable to awk

Hello, I have a file with 4 columns. An arbitrary example is shown below: a Tp 10 xyz b Tq 8 abc c Tp 99 pqr d Tp 44 rst e Tr 98 efg Based on the values in col 2 and col 3, I will execute another program. I have been running this:... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

7. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies

8. Shell Programming and Scripting

passing variable values to awk command

Hi, I have a situation where I have to specify a different value to an awk command, I beleive i have the gist of this done, however I am not able to get this correct. Here is what I have so far echo $id 065859555 This value occurs in a "pipe" delimited file in postition 8. Hence I would... (1 Reply)
Discussion started by: jerardfjay
1 Replies

9. Shell Programming and Scripting

passing awk variable to the shell script

hi; i have a file containing lines like: 1|1069108123|96393669788|00963215755711|2|0|941||;serv:Pps6aSyria;first:0;bear i want to extract the second, third and fourth record of each line and store it in a file ";" seperated this is what i wrote while read line do ... (3 Replies)
Discussion started by: bcheaib
3 Replies

10. Shell Programming and Scripting

Passing values out awk.

found that passing (input) values to awk, all work well. For example: errpt | awk 'BEGIN { errore=0 } substr($2,1,4) /'ParamData'/ { .... } ' ParamData=`date +"%m%d"` Now I wish to obtain (output) a value. Using this method is it possible to re-write ParamData, for example? Thanks in... (3 Replies)
Discussion started by: gio123bg
3 Replies
Login or Register to Ask a Question