![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk - comparing files | dbrundrett | Shell Programming and Scripting | 4 | 08-22-2008 07:23 AM |
| Comparing two files | superstar003 | Forum Support Area for Unregistered Users & Account Problems | 1 | 05-08-2008 12:34 AM |
| Comparing two files.. | padarthy | Shell Programming and Scripting | 1 | 08-29-2007 05:01 AM |
| Comparing two files... | paqman | Shell Programming and Scripting | 12 | 08-08-2007 12:45 AM |
| comparing shadow files with real files | terrym | UNIX for Advanced & Expert Users | 4 | 02-08-2007 11:38 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Comparing two files
Hi,
I have a requirement to compare two files and delete the duplicates from the master file. For eg. two files file1 and file2. Compare each row of file1 with each row file2 and delete the row from file1 if it the same as the row in file2. Hope the requirement is clear. Your early help will be appreciated. TA Narayana Gupta |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Try using a PERL script. Read both files, compare lines, delete or not, write to file, etc.
|
|
#3
|
||||
|
||||
|
Also 'man comm'
|
|
#4
|
|||
|
|||
|
grep -Fvf file1.txt file2.txt
-Renjith |
|
#5
|
||||
|
||||
|
check next post reply.
|
|
#6
|
||||
|
||||
|
I have one script which compares two files and write unique data into third file. You just have to replace third file with first one.
Code:
#!/bin/bash
clear
FILE1="/shell/file1"
FILE2="/shell/file2"
FILE3="/shell/file3"
cp /dev/null $FILE3
NOTEXIT=66
if [ ! -e $FILE1 ] && [ ! -e $FILE2 ]
then
echo "Oops! any of the file does not exist"
exit $NOTEXIT
fi
echo "Files which differs " > $FILE3
for a in `cat $FILE1 | awk '{print $9}' | sed '/^$/d'` # `cat $FILE1` `strings $FILE1 | awk '{print $9}' | sed '/^$/d'`
do
for b in `cat $FILE2 | awk '{print $9}' | sed '/^$/d'` #`strings $FILE2 | awk '{print $9}' | sed '/^$/d'`
do
if [ "$b" == "$a" ]
then
flag=0
break
else
flag=1
c="$a"
fi
done
if [ "$flag" -eq 1 ]
then
echo "$c" >> $FILE3
fi
done
for b in `cat $FILE2 | awk '{print $9}' | sed '/^$/d'` # `cat $FILE2`
do
for a in `cat $FILE1 | awk '{print $9}' | sed '/^$/d'` # `cat $FILE1`
do
if [ "$a" == "$b" ]
then
flag=0
break
else
flag=1
c="$b"
fi
done
if [ "$flag" -eq 1 ]
then
echo "$c" >> $FILE3
fi
done
echo "Result "
echo "-----------"
cat $FILE3
echo "-----------"
- nilesh |
||||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|