awk if statement in a for loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk if statement in a for loop?
# 1  
Old 08-02-2013
awk if statement in a for loop?

I need to match multiple values in a single column in a file:
example:

Source file:
Code:
abd,123,one
def,232,two
ghi,987,six

Target file:
Code:
12345,abcde,123
09876,zxvyr,566
56789,lmnop,232

Variable:
Code:
var1=`grep 2 sourcefile | awk '{print$1}'

essentially, echo "$var1" would read:
Code:
123
232

For loop:

Code:
for variable in ${var1[]}
do
cat targetfile | awk '{if ($3 == "$variable") print $1;}' > outputfile
done

in theory, the result set should be...
Code:
12345
56789

...however, when I run this, I get no errors but no output. I don't know where i've gone wrong. Any help would be greatly appriciated.

Thanks!

Last edited by Franklin52; 08-04-2013 at 10:43 AM.. Reason: Please use code tags
# 2  
Old 08-02-2013
With awk, field delimiter ,:
Code:
grep 2 sourcefile | awk -F, 'NR==FNR {A[$2]; next} ($3 in A) {print $1}' - targetfile

If NR == FNR (true for File1, here - == stdin == pipe) then store in array A, key is $2 (field 2) and jump to next line. Otherwise (not File1, i.e. in targetfile), if $3 (field 3) is in array A, print $1 (field 1)
All in one awk:
Code:
awk -F, 'NR==FNR {if ($0~search) A[$2]; next} ($3 in A) {print $1}' search=2 sourcefile targetfile

# 3  
Old 08-02-2013
I"ll give that a try, Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Bash - Nesting if statement in for loop

I have the basic command written in bash for element in 1 2 do if ]; then set el = "t" else set el = "p" fi done but i get the following error syntax error near unexpected token `for' ` for element in 1 2' What should i do differently? (3 Replies)
Discussion started by: ncwxpanther
3 Replies

3. Shell Programming and Scripting

While loop within if statement

Hi, I'm a rookie who is trying to learn this stuff. What I need help with is putting together a non complicated "while" loop within the below "if" statement. I also need the while loop to keep looping until the user types a key to end the loop. Please reveal the proper insertion points. Thank... (4 Replies)
Discussion started by: jefferj54
4 Replies

4. Shell Programming and Scripting

how to create a loop in an if statement

Hey guys, a=`cat abc | wc -l` b=`cat def | wc -l` if $a== $b then echo "a" else echo "b" fi I want the if condition to retry itself , untill a==b. I can't use goto statemt. Please help. Thanx in advance. Please use next time code tags for your code and data (5 Replies)
Discussion started by: jaituteja
5 Replies

5. Shell Programming and Scripting

Help With Loop in Case Statement script

I am writing a bash script that asks the user for input and I need it to repeat until the user selects quit.. I dont know how to write the loop for it I searched all over but i still do not get it.. if anyone could help with this it would be greatly apprciated here is my script so far: #!... (2 Replies)
Discussion started by: Emin_Em
2 Replies

6. Shell Programming and Scripting

Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message. ... (4 Replies)
Discussion started by: zero3ree
4 Replies

7. Shell Programming and Scripting

Variable problem in for loop with if statement

Hi, Again a little problem. Do not understand good why an empty string is not detected. Here is the program: #!/bin/ksh APR=`date | grep Apr | awk '{print $2$3}'` MAY=`date | grep May | awk '{print $2$3}'` JUN=`date | grep Jun | awk '{print $2$3}'` echo "Variable Apr has value:... (6 Replies)
Discussion started by: ejdv
6 Replies

8. Shell Programming and Scripting

For loop statement - catch error

I'm having a question about for loops. (bash) I have the following for example: for file in `ls *.txt` do read file ... done Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such... (4 Replies)
Discussion started by: lumdev
4 Replies

9. UNIX for Dummies Questions & Answers

if statement in a while loop

#!/usr/bin/ksh echo Please enter while read n do echo $n >> datafile done question: How can I enject an if statement that if the users enter 0 (zero) the program will exit? this is what I have but not working #!/usr/bin/ksh echo Please enter number while read n do if $n=0 then... (2 Replies)
Discussion started by: bobo
2 Replies

10. UNIX for Dummies Questions & Answers

if statement in for loop of a string

I am attempting to pass a string into awk and loop through it, and then for every occurrance of a certain character perform an action. In this case, for example, echo 1 for each time character r is found in the string. Except I can't get it to work. Could someone please tell me why? echo... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question