I have the logic down but syntax .... =(


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers I have the logic down but syntax .... =(
# 1  
Old 09-26-2008
I have the logic down but syntax .... =(

Hey guys newb here... Here is what I'm trying to do

File1
AAA
BBB
CCC

File2
xxx xxx xxx xxx xxx AAA
xxx xxx xxx xxx xxx BBB
xxx xxx xxx xxx xxx CCC

if $1 of File 1 matches $6 of File 2 then print matching $0 of File 2 .....

I've tried catting File2 into an array and ran a matching pattern to $6 on File2 but I am having serious syntax issues.... Is this a proper start?

cat File2 | awk ' {x[$0]=$6} END {if($1 = x[$0]=$6) print $0} ' File1

Im trying super hard to grasp awk but I dont think I'm built for it Smilie...... Thanks for any suggestions....Smilie
# 2  
Old 09-26-2008
if its too hard for you to grasp, here's an alternative if you have , in Python
Code:
d={}
for lines in open("file2"):
    l=lines.strip().split()
    #store last element as key , the whole line as value into lookup table d
    d[ l [-1] ] = lines.strip() 
for lines in open("file1"):
    l=lines.strip()
    print d[l]

# 3  
Old 09-26-2008
hope this will help.
Code:
while read line1;do
while read line2;do
echo "$line1line2"|awk -v var=$line2 '{if($1==$7){print var}'
done < file1
done < file2

# 4  
Old 09-28-2008
Another one:

Code:
awk 'NR==FNR{a[$1];next}$6 in a' file1 file2

Regards
# 5  
Old 09-29-2008
I tried the codes in the last 2 replys but all I get is syntax errors. Thanks for putting me in the right direction.... I will try to troubleshoot it and post my results. Smilie
# 6  
Old 09-29-2008
Quote:
Originally Posted by Unice
I tried the codes in the last 2 replys but all I get is syntax errors. Thanks for putting me in the right direction.... I will try to troubleshoot it and post my results. Smilie
Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help | Unix | grep | regular expression | backreference | Syntax/Logic

Hello, I'm working on learning regular expressions and what I can do with them. I'm using unix to and its programs to experiment and learn what my limitations are with them. I'm working on duplicating the regular expression: ^(.*)(\r?\n\1)+$ This is supposed to delete duplicate lines... (2 Replies)
Discussion started by: MykC
2 Replies
Login or Register to Ask a Question