awk command

I am making an awk command file to run on my UNIX box. I have 2 files:

file 1 file 2

1111 Jones/Ron ,P,2222
2222 Smith/John ,P,7788
3333 Smith/Jim ,P,9999
4444 Carter/Jane ,P,4444

I want to compare these 2 files. If there is anybody in file 2 that has the 3rd field match up with the field in file 1, I want to make a new file with their information.
In this example, I would have a new file (file 3) that would contain:

Jones/Ron ,P,2222
Carter/Jane ,P,4444

I want to be able to do this in the command file format:
example awk -f (name of my awk command file) File 1 > File 3

How do I do this?

Thanks,
Nick

awk -F"," 'BEGIN{ while(getline < "1" ) { arr[$0]=i++ } } { split($0, now, ","); if ( now[3] in arr ) { print } }' 2

And how would I run this in the command file format?

thanks

Sorry your question is not clear. :confused:

Are you looking for something like,

to use the command in a script.awk file... something like that..

then just include the command posted into a script.awk file.

Please forgive me for asking so many questions. I am just learning awk and I have a longgggg way to go.

Ok, so I put your script into an awk file called AWK1. I want to run it like this:

awk -f AWK1 file 2 > file 3

I want the output to go to file 3. I need file 2 to reference file 1. This would be done in your awk script. I don't see the file references in your awk script.

I put examples of file 1 and file 2 in my original post.

file 1 ==> 1
file 2 ==> 2

redirect the output of the awk command to file3

awk command > file 3

nawk -F',' -f nickg.awk file1 file2

nickg.awk:

FNR==NR {arr[$1]; next }
$3 in arr

I ended up using this: (with the help of a co-worker)

awk -f NICKAWK3 FILE1 > FILE3

Here is my NICKAWK3 file:

BEGIN {
while ((getline < "FILE2") > 0)
{
f2array[$1]=$1
}
}

{
if ($NF == f2array[$NF])
print $0
}

Thanks for all your help!!

not extremly elegant and I hope you know what it does.

It is simple and does what I need it to do. I do know what it does. I have comment lines (which I did not include) in the file so I never forget what it does. :slight_smile:

Note: we do not have the nawk command on our Unix box. I had to use awk.

Ah, a happy and a well-documented customer - the best kind!
BTW, 'nawk' stands for 'NewAWK' - most likely you can run the posted 'nawk' snippet through your own 'awk'.

I think follow can help you:

str=`cat file1`
s=`echo $str |sed 's/ //g'`
nawk -v st=$s 'BEGIN{
FS=","
}
{
if (index(st,$3)!=0)
print $0
}' file2 > file3