compare two files and make 1st file same as 2nd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two files and make 1st file same as 2nd file
# 1  
Old 08-20-2008
compare two files and make 1st file same as 2nd file

I am trying to compare two file and make changes where ever its different.

for example:

Contents of file1
IP=192.165.89.11
NM=255.255.0.0
GW=192.165.89.1

Contents of file2
IP=192.165.89.11
NM=255.255.255.255
GW=192.165.89.1

NOTE HERE THAT NM IS DIFFERENT

So i want the changes in file1
So final output for file1 should be
IP=192.165.89.11
NM=255.255.255.255
Gw=192.165.89.1


Can u help me out with the script
# 2  
Old 08-20-2008
Error

Exactly wher and how you expect the output to be..

is it like always the NM should get replace with 2nd file or is it always the file contains 3 data that needs to compare?

can you little bit more..

Thanks
Sha
# 3  
Old 08-20-2008
If you want the 2 files to be identical, why don't you just make a copy the file?

Regards
# 4  
Old 08-20-2008
thanks for ur reply

I want the output in file1
its not that NM should be replaced. It can be any of them.
Thanks
Sha[/quote]
# 5  
Old 08-20-2008
Code:
sed -e 's@^\([A-Za-z0-9]\+=\)\(.*\)@s/^\1.*/\1\2/@' file2 | sed -f - -i file1

# 6  
Old 08-21-2008
HI.. Check the below code for compare two files.

rm -f c.txt
t=`sdiff a.txt b.txt | grep "|" | wc -l`
if [ $t -eq 0 ]
then
echo "File are same"
else
echo "Hi"
sdiff a.txt b.txt | grep "|" | nawk '{
s = sprintf("%s/%s/g",$1,$3);
print s;
}' >> c.txt

cnt=1
str=" "
for i in `cat c.txt`
do
if [ "$cnt" -eq "1" ]
then
cnt=`expr $cnt + 1`
sed 's/'"$i"'' a.txt >> d.txt
else
cnt=`expr $cnt + 1`
sed 's/'"$i"'' d.txt >> d.txt
fi
done

p=`cat a.txt | wc -l | awk '{print $1}'`
echo $p
tail -$p d.txt > c.txt
rm -f d.txt
fi

Note:
Above code works only when both the files are same line.
# 7  
Old 08-22-2008
Hey u can use in simple version like this,

File.awk :
BEGIN {
FS=":"
}
FNR == NR { if (FNR==1) file1=FILENAME; arr[NR]=$0; next }
{
if ($0 != arr[FNR])
printf("%s\n", $0)
else
printf("%s\n",arr[FNR])
}

To run the above awk,

nawk -f File.awk << File 1 >> << File 2 >>

Redirect the above output to <<File 3>>

Then finally mv the File 3 to File 1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

3. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

4. Shell Programming and Scripting

Appending information from 2nd file into 1st based on intervals

Hi, I am trying to gather information from the second file and append it to the first file. input HWUSI-EAS000_29:1:100:10000:11479#0/1 + chr5 14458050 ATTGGCTGAGGTCCTACTAGTTGTGATGTGTAAGTGT HHHHHHGDGGEDGGGDGCGEDDEFFFAGE 0 second file:... (14 Replies)
Discussion started by: Diya123
14 Replies

5. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

6. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

7. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

8. Shell Programming and Scripting

Compare - 1st col of file

Hi, I have two different files, one has two columns and other has only one column. I would like to compare the first column in the first file with the data in the second file and write a third file with the data that is not present is not common to them. First file:... (26 Replies)
Discussion started by: swame_sp
26 Replies

9. Shell Programming and Scripting

file compare and make a new file

Hi, I need to make a file3 from the comparision of file1 and file2... file1 x1,x2,x3,x4 file2 y1,y2,y3,y4 if (x2=y3) then file3 x1,x3,y1,y3,y4 Thanks (11 Replies)
Discussion started by: yale_work
11 Replies

10. Shell Programming and Scripting

how to attach a variable from the 2nd file to the 1st file

Hi all, I am a newbie in unix shell script world. Say, I have a list of devices in file01 and then also have file02 with the list of the location. I need to telnet to each devices in file01 and set the location according to file02. Example: #cat file01 ttnpx01 ttnpx02 nncrd01 nncrd02... (5 Replies)
Discussion started by: lo tan
5 Replies
Login or Register to Ask a Question