compare 2 files and replace

Hi,

I have a file which contains names with counts for eg:

0622 0031 JOHN MAX 20080622003104. STAT 1.
0622 0031 BILL MAX 20080622003104. STAT 7.

and I have an exception file containing.

BILL

Can anyone help to write a script using this exception files to replace the STAT *(could be any number) to STAT 0 for the names that are in this list

the output should be and the format remains the same

0622 0031 JOHN MAX 20080622003104. STAT 0.

any comments or help appriciated.

I got a script which can do this, but it changes the format.
#!/usr/bin/ksh
nawk '
FNR==NR {ex[$1]; next}
{ $9 = ($1 in ex) ? "0." : $2; print }
' a2 a1 > a3

any help appriciated.
thanks

Use nawk or /usr/xpg4/bin/awk on Solaris:

awk 'NR==FNR{_[$1];next}$NF=$3 in _?0".":$NF' exceptions names

sorry I made a mistake in the output. the output should be

0622 0031 JOHN MAX 20080622003104. STAT 1.
0622 0031 BILL MAX 20080622003104. STAT 0.

for BILL which is found in the exception file the STAT should change from STAT 7. to STAT 0.

also the length of the line should not change during the replace..

your help is appriciated

thanks Antony

You can also do this using grep and sed

grep -f exp.txt inp.txt | sed 's/STAT [0-9][0-9]*/STAT 0/g'

exp.txt is the exception file.

Regards,
Chella

Since in your first post ti was JOHN and not BILL in the exception file the code works as expected ...

  1. If an entry in the names file matches an entry in the exception file should the stat change only if the current value is 7?
  2. You mentioned the record length and I suppose it's because of the recalculation of the current record; could you post sample of your input data using the code tags.

This worked except the output printed only the ones that were found on exception file..all the records were not printed.

Try this,

#! /bin/ksh
cp inp.txt tmp_1.txt
while read line
do
        sed "/$line/s/STAT [0-9][0-9]*/STAT 0/g" tmp_1.txt > tmp_2.txt
        cp tmp_2.txt tmp_1.txt
done < exp.txt
cat tmp_2.txt
rm tmp_2.txt tmp_1.txt

inp.txt

0622 0031 JOHN MAX 20080622003104. STAT 1
0622 0031 PETER MAX 20080622003104. STAT 3
0622 0031 BILL MAX 20080622003104. STAT 7
0622 0031 MARY MAX 20080622003104. STAT 2

exp.txt

BILL
PETER

Regards,
Chella

Thank you very much, it worked.