Computing.Net > Forums > Unix > unix script - find duplicate record

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

unix script - find duplicate record

Reply to Message Icon

Name: john
Date: July 7, 2003 at 19:52:02 Pacific
OS: os
CPU/Ram: a
Comment:

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,inactive

The 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,inactive

Thank you for your help.



Sponsored Link
Ads by Google

Response Number 1
Name: Jimbo
Date: July 7, 2003 at 21:29:02 Pacific
Reply:

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


0

Response Number 2
Name: Jimbo
Date: July 7, 2003 at 22:12:27 Pacific
Reply:

Oops, this one really works:

#! /bin/ksh -x

INPUT=file1
OUTPUT=data
cp /dev/null $OUTPUT

cat $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" >> $OUTPUT

fi

done


-jim


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


scsi tape drive doesn't w... alternative to rlogin nee...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: unix script - find duplicate record

unix script - find and replace www.computing.net/answers/unix/unix-script-find-and-replace/5816.html

Identify duplicate records in UNIX www.computing.net/answers/unix/identify-duplicate-records-in-unix/5892.html

Unix Script Error, Quel Strange www.computing.net/answers/unix/unix-script-error-quel-strange/5244.html