Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
To all unix expert,
I need your help to write a script that will identify and delete duplicate records. Examples:
File 1 (comma separated) contains:
BBB,23341,inactive
AAA,12950,active
BBB,3239,active
AAA,44098,inactive
CCC,40399,inactiveThe script will look at field 1 and if detect any rows with same field 1 content, delete the row with the string "inactive" in it. Based on the above example, tbe expected result is:
AAA,12950,active
BBB,3239,active
CCC,40399,inactiveThank you for your help.

Based on your sample input, this worked for me:
#! /bin/ksh
INPUT=file_1
OUTPUT=data.new
touch $OUTPUT
sort $INPUT | while IFS=, read COL1 COL2 COL3; do
grep $COL1 $OUTPUT || print "$COL1,$COL2,$COL3" >> $OUTPUT
done-jim

Oops, this one really works:
#! /bin/ksh -x
INPUT=file1
OUTPUT=data
cp /dev/null $OUTPUTcat $INPUT | while IFS=, read COL1 COL2 COL3; do
if [[ $COL3 = inactive ]] ; then
grep "$COL1.*,active" $INPUT || print "$COL1,$COL2,$COL3" >> $OUTPUT
else
print "$COL1,$COL2,$COL3" >> $OUTPUTfi
done
-jim

![]() |
scsi tape drive doesn't w...
|
alternative to rlogin nee...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |