The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparison Unix and Windows file sysytem localp UNIX for Dummies Questions & Answers 1 04-11-2008 01:02 AM
Comparison of two files in awk jerome Sukumar Shell Programming and Scripting 12 07-26-2006 05:16 AM
String Comparison between two files using awk rudoraj Shell Programming and Scripting 7 07-25-2006 08:04 AM
Unix comparison NewGuy100 UNIX for Dummies Questions & Answers 1 12-14-2005 12:42 PM
Unix Programming Book Comparison theicarusagenda High Level Programming 6 03-04-2005 06:30 AM

Reply
 
LinkBack Thread Tools Display Modes
  #8 (permalink)  
Old 11-12-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
You can turn around the files.
Code:
grep -vf file4 file3
Reply With Quote
Forum Sponsor
  #9 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Nov 2007
Posts: 16
Unhappy

well i tried that it is displaying
grep -vf file4 file3

mat

but criteria is that
i shud be getting rat and hat ( file 4 is assumed as updated version of file3 by the user concerned) as on comparing the 2nd and 3rd place values of file 4 do NOT match with the 2nd and 3rd place values of file3

file3
bat
mat
rat


file4
bat
rat
hat

Reply With Quote
  #10 (permalink)  
Old 11-12-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
Can you provide one sample input file each for file1 and file2. Also show us what you want in the resulting file3.
Reply With Quote
  #11 (permalink)  
Old 11-12-2007
Registered User
 

Join Date: Nov 2007
Posts: 16
Unhappy

for eg there are two files to be compared
file1
1111 | universe
2222 |good
3333 |good
4444 |good



file2
1111 | universe
3333 |universe
2222 |good
4444 |good



the contents of both the files will be compared row wise( i.e. row 1 content of file1 to be compared with row 1 content of file2 and so on)

if there is any difference/change in values (it can be numbers or those words) of corresponding rows then the script will pick up the numbers of field one(f 1) from file2 NOT file1.

like here the script should pick up 3333 and 2222 ( 2nd and 3rd positions values of file2) from file2 and redirect it to another to another third file.
Reply With Quote
  #12 (permalink)  
Old 11-12-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,037
You could read in the files simultaneously by opening them in different file descriptors, then use "read" to read from these descriptors.

Code:
exec 3</path/to/file1
exec 4</path/to/file2

typeset chBuffer1=""
typeset chBuffer2=""

while read -u3 chBuffer1 ; do
     read -u4 chBuffer2
     if [ <put your tests using chBuffer1 and chBuffer2 here> ] ; then
          ....
     else
          ....
     fi
done

exec 3<&-
exec 4<&-
bakunin
Reply With Quote
  #13 (permalink)  
Old 11-12-2007
Read Only
 

Join Date: Nov 2007
Posts: 165
Would sdiff help you?
Reply With Quote
  #14 (permalink)  
Old 11-12-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 497
Hi.

Here is a shell driver script:
Code:
#!/usr/bin/env sh

# @(#) s2       Demonstrate perl line reading, comparison.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>& 1 && version bash perl

echo

echo " Data files data1 data2:"
cat data1
echo
cat data2

echo
echo " perl script output, filtered:"
./p1 data1 data2 |
cut -d"|" -f1

exit 0
which calls a perl script:
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate display of line-by-line differences.

use warnings;
use strict;

my ($debug);
$debug = 1;
$debug = 0;

my ( $f1, $f2, $file1, $file2, $line1, $line2, $EOF1, $EOF2 );
my ($lines) = 0;

$EOF1 = $EOF2 = 0;

$file1 = shift || die " usage: $0 file_1 file2\n";
$file2 = shift || die " usage: $0 file_1 file2\n";

open( $f1, "<", $file1 ) || die " Cannot open file $f1\n";
open( $f2, "<", $file2 ) || die " Cannot open file $f2\n";

while (1) {
  if ( not( $line1 = <$f1> ) ) {
    $EOF1 = 1;
  }

  if ( not( $line2 = <$f2> ) ) {
    $EOF2 = 1;
  }

  if ( $EOF1 + $EOF2 != 0 ) {
    print STDERR " Checking EOF on both files.\n" if $debug;
    if ( $EOF1 == 0 ) {
      print STDERR " Note - file $file1 has extra lines, stopping.\n";
    }
    if ( $EOF2 == 0 ) {
      print STDERR " Note - file $file2 has extra lines, stopping.\n";
    }

    # In any case, this is our last read
    last;
  }

  $lines++;
  if ( index( $line1, $line2 ) != 0 ) {
    print $line2;
  }
}

print STDERR " ( Complete pairs of lines read: $lines )\n";

exit(0);
Producing from your data on files data1 and data2:
Code:
% ./s2

(Versions displayed with local utility "version")
GNU bash 2.05b.0
perl 5.8.4

 Data files data1 data2:
1111 | universe
2222 |good
3333 |good
4444 |good

1111 | universe
3333 |universe
2222 |good
4444 |good

 perl script output, filtered:
 ( Complete pairs of lines read: 4 )
3333
2222
cheers, drl
Reply With Quote
Google UNIX.COM
Reply

Tags
diff, grep

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0