Need help in setting up a script..

Hi
i need to have a update for a file that updates only the last modified content from another file....

say for example....if file A is having content like
.
.
.
hatxxxx
catxxxx
ratxxxx
and file B(the file to be updated is having content as
hatxxxx
catxxxx
then file B has to be updated/appended with the last modified content(i.e rat in this case) so as to look like
hatxxxx
catxxxx
ratxxxx
plz help someone with a script.

My understanding of what you are after:
Scan through file A looking for a block of text identical to the entire contents of file B.
Once found, read everything after that block of text in file A and append it to file B.

Right?

#!/bin/sh
size=`wc -c $2 | awk '{ print $1 }'`
offset=0
while true
do
  dd if=$1 of=temp.$$ bs=1 skip=$offset count=$size > /dev/null 2>&1
  if diff temp.$$ $2 > /dev/null 2>&1
  then
    offset=`expr $offset + $size`
    dd if=$1 bs=1 skip=$offset >> $2
    rm temp.$$
    exit
  else
    offset=`expr $offset + 1`
  fi
done

Usage: scriptname.sh fileA fileB
(Appends to fileB)

Thanks...exactly what i require...excellent job.

need some clarification....what does this statement do dd if=$1 of=temp.$$ bs=1 skip=$offset count=$size > /dev/null 2>&1

just to get you started, here's one way.

# comm -3 file file1
ratxxxx

Then you can just pipe this output to B.

Can i do the same with diff command...
what -3 in that command do?

yes. should be able to . check the man page for diff

man comm.

and this command did the trick...comm -3 file1 file2 >> file2
thanks

dd is a method of copying data around, if= is where you specify the file (or device) to read from, of= is for the output file (or device) bs=1 means a block size of 1 byte (ie we read byte by byte), skip= says how many blocks to skip before beginning the copy, count= says how many blocks to copy.

man dd for more information.

I just had a play with this on Solaris, it looks like comm works a little differently.

On your system, with the above example, does comm -3 file1 file2 produce the string 'ratxxxx' even if file1 has a lot of lines at the start (ie before 'hatxxxx')?

nope. I assume file1 and file2 are similar , only difference is file2 is always later than file1. :slight_smile:

In that case:

cp file1 file2

:wink:

lol, true. sometimes we forget the basic things