Passing shell variable to awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing shell variable to awk script
# 1  
Old 02-26-2012
Passing shell variable to awk script

I want to pass a shell variable to awk script :

Code:
# cat file
PSAPSR3                           3722000   91989.25          2         98
PSAPSR7                           1562000 77000.1875          5         95
PSAPUNDO                            92000  4087.5625          4         96

# NU="PSAPSR7";export NU
 
# awk '$1 == "$NU" {print}' file

But above command is not working...??? Smilie Smilie Smilie

Expected output :

Code:
PSAPSR7                           1562000 77000.1875          5         95

Thanks...

Last edited by radoulov; 02-26-2012 at 03:46 AM.. Reason: Code tags!
# 2  
Old 02-26-2012
why not going with:
Code:
awk '$1=="PSAPSR7"' file

# 3  
Old 02-26-2012
Thanks Pandeesh,

But I can't specify the hardcoded string, insted I need to use the variable only and provide value to awk command through the variable. It's script requirement.
# 4  
Old 02-26-2012
this works:
Code:
pandeeswaran@ubuntu:~$ awk -v a=$NU '$1==a' file
PSAPSR7 1562000 77000.1875 5 95
pandeeswaran@ubuntu:~$

This User Gave Thanks to pandeesh For This Post:
# 5  
Old 02-26-2012
Thanks Pandeesh,

These codes working the way I wanted...!!!



Cheers.....!!!
# 6  
Old 02-26-2012
instead of using -v switch to pass the SHELL variable to the awklet ,you can use like below .

Code:
awk '$1==a' a=$NU input-file


Last edited by codemaniac; 02-26-2012 at 04:04 AM.. Reason: correction
This User Gave Thanks to codemaniac For This Post:
# 7  
Old 02-26-2012
Is there any difference in performance between -v switch and awklet?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Passing value of variable to a query within shell script

I have a script in which i connect to database to run a query and get the result of the query to a temp file. This works fine , now what i want is there is flat file which contains the value to be used in the query. I want to read this file line by line and then run the query for each value in that... (7 Replies)
Discussion started by: gpk_newbie
7 Replies

3. Shell Programming and Scripting

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: @ 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... (3 Replies)
Discussion started by: Malavin
3 Replies

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

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

6. Shell Programming and Scripting

Passing a variable from shell script to mysql query?

I heard this was possible but from my research I haven't been able to figure it out yet. Seems it should be simple enough. Basically from a high level view I'm trying to accomplish... . $X='grep foo blah.log' then 'mysql command SELECT foo FROM bar WHERE ' . $X or something like that. ... (2 Replies)
Discussion started by: kero
2 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

error in passing a variable to sqlplus from a shell script

hi, I am using a shell script from where i will be conecting to sqlplus.. i am having a problem in passing a variable to sqlplus query.. i will be assigning the variable in the unix environment..whenever i am trying to pass a variable having the contents greater than 2500 characters, i am... (3 Replies)
Discussion started by: kripssmart
3 Replies

9. UNIX for Advanced & Expert Users

Passing a variable into an awk script

Hello all, I'm trying to run a script of this format - for i in $(cat <file>); do grep $i <file1>|awk '{print $i, $1, $2}' It's not working - does anyone know how this can be done? Khoom (5 Replies)
Discussion started by: Khoomfire
5 Replies

10. 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
Login or Register to Ask a Question