How to Compare Two Text Files for The Same Exact Text Using Bash

How to compare two text files for the same exact text using BASH?

Assuming that order does not matter,

grep -F -f FILE2 FILE1

should do the trick. (This works because of a little-known fact: the -F option to grep doesn't just mean "match this fixed string," it means "match any of these newline-separated fixed strings.")

find difference between two text files with one item per line

You can try

grep -f file1 file2

or

grep -v -F -x -f file1 file2

Bash script to compare two text files

Why can't you use built-in diff command like

diff file1.txt file2.txt

Compare two text files line by line, finding differences but ignoring numerical values differences

Would you please try the following:

COMPARE_FILES() {
awk '
NR==FNR {a[FNR]=$0; next}
{
b=$0; gsub(/[0-9]+/,"",b)
c=a[FNR]; gsub(/[0-9]+/,"",c)
if (b != c) {printf "< %s\n> %s\n", $0, a[FNR]}
}' "$1" "$2"
}

compare two files and get the output for the same lines

From the grep manpage:

   -f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)

Therefore:

grep -f file1 file2

domain1.com - site110
domain5.com - site120

Compare two files line by line and generate the difference in another file

diff(1) is not the answer, but comm(1) is.

NAME
comm - compare two sorted files line by line

SYNOPSIS
comm [OPTION]... FILE1 FILE2

...

-1 suppress lines unique to FILE1

-2 suppress lines unique to FILE2

-3 suppress lines that appear in both files

So

comm -2 -3 file1 file2 > file3

The input files must be sorted. If they are not, sort them first. This can be done with a temporary file, or...

comm -2 -3 <(sort file1) <(sort file2) > file3

provided that your shell supports process substitution (bash does).

Fastest way to tell if two files have the same contents in Unix/Linux?

I believe cmp will stop at the first byte difference:

cmp --silent $old $new || echo "files are different"

Shell Script compare file content with a string

Update: My original answer would unnecessarily read a large file into memory when it couldn't possibly match. Any multi-line file would fail, so you only need to read two lines at most. Instead, read the first line. If it does not match the string, or if a second read succeeds at all, regardless of what it reads, then send the e-mail.

str=ABCD
if { IFS= read -r line1 &&
[[ $line1 != $str ]] ||
IFS= read -r $line2
} < test.txt; then
# send e-mail
fi

Just read in the entire file and compare it to the string:

str=ABCD
if [[ $(< test.txt) != "$str" ]]; then
# send e-mail
fi

How to compare filenames in two text files on Linux bash?

An awk solution might be more efficient for this task:

awk '
{ f=$0; sub(/\.(xy?|yx?|jpg)$/,"",f) }
NR==FNR { a[f]; next }
!(f in a)
' list1.txt list2.txt > result.txt

Compare two files, and keep only if first word of each line is the same

Use awk where you iterate over both files:

$ awk 'NR == FNR { a[$1] = 1; next } a[$1]' a.txt b.txt
hello dolly 1
tom sawyer 2
super man 4

NR == FNR is only true for the first file making { a[$1] = 1; next } only run on said file.



Related Topics



Leave a reply



Submit