comparing two files yy.unl and xx.dat

Loren M. Lang lorenl at alzatex.com
Fri Dec 10 00:47:50 PST 2004


On Thu, Dec 09, 2004 at 03:39:20PM -0500, Thiyagarajan, Jemima wrote:
> Hi all
> 
> I am new to shell scripts. I have a doubt. Please somebody help me.
> 
> Any explanation with example will be appreciated.
> 
> I try to compare 2 files xx.dat (data separated by comma) and yy.unl
> (data separated by |).
> 
> And if a record in xx.dat is not found in yy.unl then move that record
> to some database table like db2 table.

You could use cut on one file to seperate each field to a seperate file,
then use paste to put it back together with the right seperator.  After
that sort both files and the run a diff, grepping for only deleted lines
of one file:

man cut
man paste
man sort
man diff
man grep

$ cat xx.dat
me,20,aax
you,32,fds
them,12,wsx

$ cat yy.unl
us|45|wrt
me|20|aax
them|12|wsx

$ cut -d'|' -f1 yy.unl > field1
$ cut -d'|' -f2 yy.unl > field2
$ cut -d'|' -f3 yy.unl > field3

$ paste -d, field1 field2 field3 | sort > tmp

$ cat xx.dat | sort | diff - tmp | grep '^<' | cut -d' ' -f2 > db2

$ cat db2
you|32|fds

$ diff xx.dat db2 | grep '^<' | cut -d' ' -f2 > new-xx.dat
$ mv new-xx.dat xx.dat
$ cat xx.dat
me,20,aax
them,12,wsx

But perhaps sed, awk or perl would be better.

> 
>  
> 
> Thanks
> 
>  
> 
> Jemima 
> 
>  
> 
> 
> 
> 
> -----------------------------------------
> This message and its contents (to include attachments) are the property of
> Kmart Corporation (Kmart) and may contain confidential and proprietary
> information. You are hereby notified that any disclosure, copying, or
> distribution of this message, or the taking of any action based on
> information contained herein is strictly prohibited. Unauthorized use of
> information contained herein may subject you to civil and criminal
> prosecution and penalties. If you are not the intended recipient, you
> should delete this message immediately.
> 
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"

-- 
I sense much NT in you.
NT leads to Bluescreen.
Bluescreen leads to downtime.
Downtime leads to suffering.
NT is the path to the darkside.
Powerful Unix is.

Public Key: ftp://ftp.tallye.com/pub/lorenl_pubkey.asc
Fingerprint: B3B9 D669 69C9 09EC 1BCD  835A FAF3 7A46 E4A3 280C
 


More information about the freebsd-questions mailing list