Checking the equality of part of two files

Is it possible to check if the first line of two files is equal with diff(or another simple bash command)?

[Usually checking the equality of the first / last lines kor even lines ij]

+5
source share
4 answers

To distinguish the first k lines of two files:

$ diff <(head -k file1) <(head -k file2)

Similary to distinguish the last k lines:

$ diff <(tail -k file1) <(tail -k file2)

To distinguish strings from i to j:

diff <(sed -n 'i,jp' file1) <(sed -n 'i,jp' file2)
+9
source

My solution seems pretty basic and beginner compared to the dog above, but it doesn’t matter here!

echo "Comparing the first line from file $1 and $2 to see if they are the same."

FILE1=`head -n 1 $1`
FILE2=`head -n 1 $2`

echo $FILE1 > tempfile1.txt
echo $FILE2 > tempfile2.txt

if diff "tempfile1.txt" "tempfile2.txt"; then
    echo Success
else
    echo Fail
fi
+1
source

filterdiff patchutils. file1 file2 j k:

diff -U 0 file1 file2 | filterdiff --lines j-k
+1

.

krithika.450> head -1 temp1.txt temp4.txt
==> temp1.txt <==
Starting CXC <...> R5x BCMBIN (c)  AB 2012

==> temp4.txt <==
Starting CXC <...> R5x BCMBIN (c)  AB 2012

The below command displays yes if the first line in both files is equal.

krithika.451> head -1 temp4.txt temp1.txt | awk '{if(NR==2)p=$0;if(NR==5){q=$0;if(p==q)print "yes"}}'
yes
krithika.452> 
0
source

All Articles