compare two txt files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two txt files
# 1  
Old 09-20-2006
how compare line1 from file1 with line1 from file2

i have two files

file1
-----
absgsh jshsh
shshhs hshhs

file2
------
kakkaka iurir
brbrbrbr rjrbjrhjkehr rgjhergrhg hrghrgh jrhgrjg

i want bash shell script to campare line1-file1 - line-file2 line2-file1 -line2 file2 ........ ( without diff , cmp , comm )

thanks...

Last edited by space13; 09-25-2006 at 06:03 PM..
# 2  
Old 09-20-2006
maybe 'man comm'....
# 3  
Old 09-21-2006
Hi

can you check if the below is useful

to run the below script

ex: diff.sh <oldfile> <newfile>

#!/bin/sh
#
diff -b -w ${1} ${2} > ${1}.diff
if [[ $? -eq 0 ]] ; then
echo "Added: "
diff -b -w ${1} ${2} | grep "^< "
echo "Removed: "
diff -b -w ${1} ${2} | grep "^> "
else
echo "Files were added/removed"
fi;
exit < ${1}.diff

rm ${1}.diff
# 4  
Old 09-21-2006
Quote:
Originally Posted by space13
how compare two txt files line by line without use diff or cmp or comm commands? help me plz...
Not using diff,comm,cmp?? here's one example in Python

Code:
import filecmp
if filecmp.cmp("file1.txt","file2.txt") :
   print "they are equal"
else:
   print "they are not equal"

# 5  
Old 09-21-2006
why don't you you want to use these commands ?

Jean-Pierre.
# 6  
Old 09-21-2006
alternative in Perl

You can use the following Perl script to compare two files. But please note that such practice should not be encouraged. When you already have the commands to do the job, writing your own script to do the same is not recommended.

#!/usr/bin/perl
# file_compare.pl
# Purpose: compare two files and show differences
# usage: file_compare.pl filename1 filename2

use strict;
use warnings;

my $file1 = shift or die "filename missing \n";
my $file2 = shift or die "filename missing \n";

open (FILE1, "< $file1") or die "Can not read file $file1: $! \n";
my @file1_contents = <FILE1>; # read entire contents of file
close (FILE1);

open (FILE2, "< $file2") or die "Can not read file $file2: $! \n";
my @file2_contents = <FILE2>; # read entire contents of file
close (FILE2);

my $length1 = $#file1_contents; # number of lines in first file
my $length2 = $#file2_contents; # number of lines in second file

if ($length1 > $length2) {
# first file contains more lines than second file
my $counter2 = 0;
foreach my $line_file1 (@file1_contents) {
chomp ($line_file1);

if (defined ($file2_contents[$counter2])) {
# line exists in second file
chomp (my $line_file2 = $file2_contents[$counter2]);

if ($line_file1 ne $line_file2) {
print "\nline " . ($counter2 + 1) . " \n";
print "< $line_file1 \n" if ($line_file1 ne "");
print "--- \n";
print "> $line_file2 \n\n" if ($line_file2 ne "");
}
}
else {
# there is no line in second file
print "\nline " . ($counter2 + 1) . " \n";
print "< $line_file1 \n" if ($line_file1 ne "");
print "--- \n";
print "> \n"; # this line does not exist in file2
}
$counter2++; # point to the next line in file2
}
}
else {
# second file contains more lines than first file
# or both have equal number of lines
my $counter1 = 0;
foreach my $line_file2 (@file2_contents) {
chomp ($line_file2);

if (defined ($file1_contents[$counter1])) {
# line exists in first file
chomp (my $line_file1 = $file1_contents[$counter1]);

if ($line_file1 ne $line_file2) {
print "\nline " . ($counter1 + 1) . " \n";
print "< $line_file1 \n" if ($line_file1 ne "");
print "--- \n";
print "> $line_file2 \n" if ($line_file2 ne "");
}
}
else {
# there is no line in first file
print "\nline " . ($counter1 + 1) . " \n";
print "< \n"; # this line does not exist in file1
print "--- \n";
print "> $line_file2 \n" if ($line_file2 ne "");
}
$counter1++; # point to next line in file1
}
}
# 7  
Old 09-21-2006
Code:
awk '
FILENAME == "f1" { arr[$0]++}
FILENAME == "f2" { if ( arr[$0] != 1 ) { print "f2:" $0 ;delete arr[$0]}
                  else  delete arr[$0]
                }
END {
for ( key in arr ) print "f1:" key
}' f1 f2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and compare values from different txt files

Hello, i am new in Bash. Actually i have a directory : /home/resultfiles and inside i have these txt files: 531_1.out.res, 531_2.out.res , 531_3.out.res 532_1.out.res, 532_2.out.res , 532_3.out.res 533_1.out.res, 533_2.out.res, 533_3.out.res All these txt files has this format : num_q all... (3 Replies)
Discussion started by: nimpoura
3 Replies

2. Shell Programming and Scripting

Compare two txt files,mismatches will be in new txt files

Hi, Below are the sample data for txt files. txt file 1 Partnumber|catgroup_id 10001082|46016 10001093|4680 10001093|386003 10001093|463004 10003251|683 10003251|63005 10003252|463005 10003252|4683 10003260|463005 10003260|4683 10003264|4683 10003264|463005 13420000|67... (5 Replies)
Discussion started by: Ankita Talukdar
5 Replies

3. UNIX for Dummies Questions & Answers

How to tar all files except logs and .txt files

Hi I greatly appreciate the forum and the people here very helpful :) I could able to tar all the directories, sub dirs and the files. But unable to tar all the directories excluding the log and text files. Can anyone please advise the exact command to use for excluding .log and .txt? PS:... (4 Replies)
Discussion started by: Olivia
4 Replies

4. Shell Programming and Scripting

Using csh / awk / sed to compare database sizes in a txt file

Hello, I have an output file showing database sizes across the 3 environments that I use (LIVE, TEST & DEVELOPMENT). I am trying to write a script that lets me know if the size of a db on one environment is different to its corresponding db on the other environments. Here is an example... (4 Replies)
Discussion started by: stevie_g
4 Replies

5. Shell Programming and Scripting

moving the files in a.txt files to a different directory

HI All, I am coding a shell script which will pick all the .csv files in a particular directoryand write it in to a .txt file, this .txt file i will use as a source in datastage for processing. now after the processing is done I have to move and archive all the files in the .txt file to a... (5 Replies)
Discussion started by: subhasri_2020
5 Replies

6. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

7. Shell Programming and Scripting

Compare two txt files

Hi, I want to compare two txt files and output the matches to a file. For example I have FILE1 - accounts1.txt Peter norman elektra table CHAIR Newman nOvice FILE2 - accounts2.txt noland trainfil peter Newman norman neverlan CHAIR jackson KelSo (4 Replies)
Discussion started by: novice_sn6
4 Replies

8. Solaris

list files .txt and .TXT in one command

Dear experts, In a directory i have both *.TXT and *.txt files. I have a script- for file in `ls *.txt`; do mv $file /tmp/$file How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find. br//purple (4 Replies)
Discussion started by: thepurple
4 Replies

9. UNIX for Dummies Questions & Answers

Compare and Remove duplicate lines from txt

Hello, I am a total linux newbie and I can't seem to find a solution to this little problem. I have two text files with a huge list of URLS. Let's call them file1.txt and file2.txt What I want to do is grab an URL from file2.txt, search file1.txt for the URL and if found, delete it from... (11 Replies)
Discussion started by: rmarcano
11 Replies

10. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies
Login or Register to Ask a Question