Using two shell variables in single AWK statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using two shell variables in single AWK statement
# 1  
Old 09-15-2008
Using two shell variables in single AWK statement

meas is a shell variable, and this works perfectly fine for me:

awk -v var=$meas -F, '$1==var' /abcd/efgh.txt > temp1.csv

However, i want to introduce another shell variable, named, defnfile in the statement, in place of hardcoded path for efgh.txt like:

awk -v var=$meas -F, '$1==var' $defnfile > temp1.csv

This does not work..

So how does one define two shell variables in a single awk statement?
# 2  
Old 09-15-2008
Quote:
[...]However, i want to introduce another shell variable, named, defnfile in the statement, in place of hardcoded path for efgh.txt like:

awk -v var=$meas -F, '$1==var' $defnfile > temp1.csv

This does not work..
What's the error message that you get?

P.S. Quoting the variables is a good habit:

Code:
awk -v var="$meas" -F, '$1==var' "$defnfile" > temp1.csv

# 3  
Old 09-15-2008
Its not fetching the correct value.
defnfile is a shell variable, not awk variable...
# 4  
Old 09-15-2008
You're using it as a shell (not AWK) variable in the command you posted.
What's the output from:

Code:
printf "%s\n" "$defnfile"
awk '{ print FILENAME; exit } "$defnfile"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single quotes insert statement using awk

Hi, Need help, using awk command to insert statement awk -v q="'" '{ print "db2 connect to repolab > /dev/null; " "\n" "db2 -x \" select name from IBMPDQ.PROFILE where managed_database = " q $1"_"$3"__0" q "\"" } ' profile.txt | sh - | awk -v i="'" ' { print "db2 connect to repolab >... (1 Reply)
Discussion started by: Mathew_paul
1 Replies

2. Shell Programming and Scripting

Can we convert 3 awk statements in a single statement

Hi, Can we use 3 statements convert in a single statement. First statement output using the second statement and the second statement output using the third statement please let me know the syntax so that I can able to merge all the three statement. (2 Replies)
Discussion started by: Priti2277
2 Replies

3. Shell Programming and Scripting

Using Shell variables in awk

Hi All, I have a file which I am reading and looking for "EXIT" statement. I want to insert ":JCWPROD" after each EXIT statement only if ":JCWPROD" doesn't exist. Here is the sample file : EXIT Testing EXIT tesing123 EXIT Desired file : EXIT :JCWPROD Testing EXIT :JCWPROD... (7 Replies)
Discussion started by: nua7
7 Replies

4. UNIX for Dummies Questions & Answers

variables in awk statement

Hi, i can print 2nd, 4th and 6th columns in a file using the shell command: awk -F "|" '{print $2 $4 $6}' file1.txt Now, the position and number of columns can be variable and from a variable POSLIST So let's say POSLIST='$2 $4 $6' when i use the command: a) awk -F "|" '{print... (2 Replies)
Discussion started by: ysrini
2 Replies

5. Shell Programming and Scripting

awk - take variables out to shell

Hi, How could we take the value of awk variables out to shell? I know the following methods 1. awk '{print $1}' < file | read a echo $a 2. a=`awk '{print $1}' < file` echo $a Please let me know if there are any other methods. Also, how do we take more than 1 variable value... (4 Replies)
Discussion started by: Thumban
4 Replies

6. Shell Programming and Scripting

awk pattern match and search in single statement

Hi All, I am trying to alter all lines between EXEC SQL and END-EXEC that have an INCLUDE in them. The following code search="INCLUDE " cp -f ${WORK}/$file.in ${WORK}/$file.wrk2 for item in `echo $search `; do > ${WORK}/$file.wrk1 awk -vITEM="$item" '{ if ( $0... (3 Replies)
Discussion started by: Bruble
3 Replies

7. Shell Programming and Scripting

AWK: replace single positional character given variables

I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk. Story: I'm automating the (re)configuration of network interfaces,... (3 Replies)
Discussion started by: System Shock
3 Replies

8. Shell Programming and Scripting

single quotes in awk statement

Hi, I have written a code to modify a string say, StringA=abc,def,ghi I need to change it to something like: StringB=This means abc='ABC', This mean def='DEF', This means ghi= 'GHI' StringB=$(echo $StringA | awk -F',' 'BEGIN { OFS="," } { for (i=1; i<=NF;i++) $i="This means... (2 Replies)
Discussion started by: tostay2003
2 Replies

9. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies

10. Shell Programming and Scripting

Using shell variables In awk

Oh its not my day for syntax... cat gzipsize.txt | awk '{print "echo",$1,$2} > master.txt I have read a lot about the awk -v but haven't been able to get it to work. I have a variable in my script and I'm looking just to push it into the awk after the $2 (or anywhere would do)!!! Every... (11 Replies)
Discussion started by: nortypig
11 Replies
Login or Register to Ask a Question