KSH: Compare variable to $1 in an input file

Hello,

I am working with KSH on AIX and I have 2 files generated from different sources... as seen below:

FILE1 FILE2
AAA AAA@ABS0001C
BBB BBB@ABS0003D
CCC CCC@ABS0023A
DDD DDD@ABC0145D
EEE EEE@ABS0090A
FFF FFF@ABS0002A
GGG GGG@ABC0150D
HHH

[note FILE1 is noted above by the 3 characters, FILE2 is everything after the space]

FILE1 is main main data source, created daily and changes quite frequently. FILE2 is static and requires manual update. Each record in FILE1 will match the characters before the @ symbol in FILE2. However, at times, FILE1 may have more records than FILE2 and there will be no match. Rarely, FILE1 will contain less records than FILE2

I need a way to take the value of each line in FILE1 and search for it in FILE2... if found, then I need to print the record from FILE1 with the text after the @ symbol in FILE2. If no match found, I need to print "NULL"

Example output:

AAA ABS0001C
BBB ABS0003D
CCC ABS0023A
DDD ABC0145D
EEE ABS0090A
FFF ABS0002A
GGG ABC0150D
HHH NULL

I'm able to put the static file within my script in the form of a giant case statement, but that is really not a feasible approach as there are 200+ records in the file. I also think I could do this with a few nasty loops, but I'm really looking for an efficient practical approach. Thanks for any help you can give...

nawk -F'@' 'FNR==NR{f2[$1]=$2;next} {print $1, (($1 in f2)?f2[$1]:"NULL")}' file2 file1
1 Like
grep -f file1 <file2 | tr "@" " "

the above works up to the printing of NULL... gonna have to think about that.

The following may help you solve your problem:

join -t@ -a1 -eNULL -o 0,2.2 f1 f2 | tr @ ' '

Note: join requires pre-sorted files (such as your sample data).

Sample run:

$ cat f1
AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH
$ cat f2
AAA@ABS0001C
BBB@ABS0003D
CCC@ABS0023A
DDD@ABC0145D
EEE@ABS0090A
FFF@ABS0002A
GGG@ABC0150D
$ join -t@ -a1 -eNULL -o 0,2.2 f1 f2 | tr @ ' '
AAA ABS0001C
BBB ABS0003D
CCC ABS0023A
DDD ABC0145D
EEE ABS0090A
FFF ABS0002A
GGG ABC0150D
HHH NULL

Regards,
Alister

I just tested the 'nawk' statement and it worked perfectly... thank you so much for the responses!

Cheers