Help regarding files

Hi,

I want my script to get the updated lines from a file to another file for each time the script runs.

EX: file1 will be updated time to time(no particular interval can determined, it can update at any time). In my script i want to run a command so that everytime the script runs, it should get the updated lines to a tmp file. If there are no updates, no tmp file should be created.

My script runs every 30 mins.

Could any one guide me so that i'd start this?

Cheers,
Sai

one idea: ( not tested)

tmpfile=file.tmp
sourcefile=file.main
 
lc_main=$(wc -l file.main | awk '{print $1}')
lc_tmp=$(wc -l file.tmp | awk '{print $1}')
 
 
lc_diff=$(echo "$lc_main-$lc_tmp" | bc)
 
tail +${lc_diff} $sourcefile >> $tmpfile

You can try wat Anchal has suggested

hi Sai,

Put below code in your script and check.
suppose OLD_FILe is a file which you are going to update.

cp -i OLD_FILe Newfile
###after updation of OLD_FILe#####
diff OLD_FILe Newfile >tempfile

VARIABLE=`cat tempfile|wc -l`
if [ "$VARIABLE" -eq 0 ]
then
echo "No changes"
rm -F tempfile
fi
cat tempfile
rm -F Newfile
___________________________________________________
I am new to UNIX programming but i thiknk this should work.

Thanks for the replies.
@anchal: The code gets the diff of lines(say n lines) but tail will not print all the n lines but it will print the nth line. Rest of the things are good and i am working on this.

@sam: Many thanks for the reply. I feel some editing work needs to be done after the diff command. However, i will try to implement this too.

Cheers,
Sai

---------- Post updated at 08:43 AM ---------- Previous update was at 07:07 AM ----------

Hi anchal...

I have modified your code and tried to get through this - by the end of script i will be with a file which contains only modified lines.

#!/bin/bash
tmpfile=file.tmp
sourcefile=file.main
tmpfile1=file1.tmp
 
rm -f $tmpfile1
 
lc_main=$(wc -l file.main | awk '{print $1}')
lc_tmp=$(wc -l file.tmp | awk '{print $1}')
 
lc_diff=$(echo "$lc_main-$lc_tmp" | bc)
lc_diff1=$(echo "$lc_main-$lc_diff" | bc)
 
#lc_mdiff=$(echo "$lc_diff+$lc_diff1" | bc)
 
i=1
sed "${i},${lc_diff1}d" $sourcefile >> $tmpfile
sed "${i},${lc_diff1}d" $sourcefile > $tmpfile1
 
#tail +${lc_diff} $sourcefile >> $tmpfile
 

From the above code, for the each run of the script there will be a file(tmpfile1 in script) with ONLY latest updates.
I'd be happy to receive some more ideas regarding this, which would make my task more easier and perfect.

Looking forward for more help!!!
Sai