awk == with a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk == with a variable
# 1  
Old 09-25-2012
awk == with a variable

korn shell script on a Solaris 10 system

Code:
for NUM in 1234 1235; do cat <file> |awk -F\; '$7==$NUM {print $0}' ; done

If the numbers 1234 or 1235 appear in the 7th column, print the line

this works
Code:
for NUM in 1234 1235; do cat <file> |awk -F\; '$7==1234 {print $0}' ; done

but when I try to put a variable in it doesn't like it. I have tried to protect my $NUM variable using ${NUM" or $"{NUM}" but no joy.

any ideas?
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 09-25-2012 at 02:55 PM.. Reason: code tags, please!
# 2  
Old 09-25-2012
Code:
for NUM in 1234 1235
do
   nawk -F\; '$7==n' n="${NUM}" myFile
done

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 09-25-2012
Thing I don't like about this is your file is being read once for each value of NUM. Where are these NUM values comming from (hardcoded, output of some other command, commandline input, etc)?

If we knew that it should be possible to get a nawk command then reads the file once and prints any line that matches your list of required values.

for example:
Code:
nawk '$7 ~ N' N="1234|1235" myFile

# 4  
Old 09-25-2012
Not sure if this works with korn on solaris, but you could build a regex with your numbers and have awk compare $7 against it:
Code:
NUM=1234\|1235;awk  '$7~/'$NUM'/ {print $0}' file

Of course you can drop the {print $0}, as Chubler_XL posts...

Last edited by RudiC; 09-25-2012 at 04:15 PM.. Reason: Improve as Chubler_XL says...
# 5  
Old 10-05-2012
Here is what my final code looks like once I had cleaned it up
Code:
 nawk -F\; '$7==n' n="${NUM}" file

$7 is the column I want to search for a specific number. This number may exist in other columns in the file so I want to be specific that the $NUM must be in the 7th position.
$NUM contains a list of numbers I want to search for
This User Gave Thanks to snoman1 For This Post:
# 6  
Old 10-05-2012
You cannot use == on a list of numbers. It will check if column 7 is equal to "1234 1235", see that it isn't, and return false. You'll have to split it in the BEGIN section. If you put it in an array like B["12345"]=1, you can check without a loop.

Since you need it in the BEGIN section, you will have to put the variable in with -v before, not VAR= afterwards. When you put in variables at the end, they get assigned when awk starts reading files, after BEGIN happens.

Code:
awk -v N="1234 5678" 'BEGIN { split(N,X); for(L in X) B[A[X]]=1 } $7 in B' inputfile


Last edited by Corona688; 10-05-2012 at 03:41 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 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. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

10. AIX

really stuck- need to get a variable within a variable- AWK

Hi all, I have been struggling with this all day, and it is key to a conversion database I have to write. The data converts the information out of an array using AWK, and basically all I have to do is figure out how to get the value of a variable inside a variable. Right now at its... (11 Replies)
Discussion started by: jeffpas
11 Replies
Login or Register to Ask a Question