I have 2 text files that I need to compare line by line.
I'm basically wanting to output either "matching" or "not matching" for each line depending on if it matches.
I've tried reading a few tutorial and using stuff like diff
and dircmp
but can't seem to find a way to do this. I don't care if it's bash, perl, python, etc. Both files are 243 lines.
Is there a command available in Linux to do this?
Here's an example of what I'm looking for...
File 1
Test
Hello
Example
File 2
Test
What
Example
And I'd want to output this:
matching
not matching
matching
In perl:
#!/usr/bin/perl
use strict;
use File::Slurp;
my @file1 = read_file 'file1', { chomp => 1 };
my @file2 = read_file 'file2', { chomp => 1 };
foreach (@file1) {
my $line = shift @file2;
print $_ eq $line ? "not matching\n" : "matching\n";
}
What you are after is an awk script of the following form:
$ awk '(NR==FNR){a[FNR]=$0;next}
!(FNR in a) { print "file2 has more lines than file1"; exit 1 }
{ print (($0 == a[FNR]) ? "matching" : "not matching") }
END { if (NR-FNR > FNR) print "file1 has more lines than file2"; exit 1}' file1 file2
This script works on the basis that both of your files are 243 lines. You will need to sort both files before running the script ie sort file1.txt > file1.sorted.txt
and the same for the other file.
#!/bin/bash
while read file1 <&3 && read file2 <&4
if [[ $file1 == $file2 ]]; then
echo "matching" >> three.txt
else
echo "not matching" >> three.txt
fi
done 3</path/to/file1.sorted.txt 4</path/to/file2.sorted.txt
The above script will read each file line by line, comparing the input using the if statement. If the two strings are identical, it will write "matching" to three.txt
else it will write "not matching" to the same file. The loop will go through each line.
You will have to sort the data within both files to make a comparison. I've tested it with the following data:
one.sorted.txt
abc
cba
efg
gfe
xyz
zxy
two.sorted.txt
abc
cbd
efh
gfe
xyz
zmo
three.txt
matching
not matching
not matching
matching
matching
not matching
Its best to use dedicated linux file comparing tools such as Meld or Vimdiff, they are pretty straight forward and very convinient.
You can enter 'which meld' to check if you have it installed, if not found, install using this:
sudo apt-get install meld
In addition, here is a simple python script to get the results you asked for:
#!/usr/bin/env python3
with open ('1.txt') as f1:
lines1 = f1.readlines()
lines1 = [line.rstrip() for line in lines1]
with open ('2.txt') as f2:
lines2 = f2.readlines()
lines2 = [line.rstrip() for line in lines2]
for i, line in enumerate(range(min(len(lines1),len(lines2)))):
print("matching") if lines1[i] == lines2[i] else print("not matching")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.