The UNIX and Linux Forums  

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



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #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: 580
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