Compare lines within a file

could someone write a short script which is able to take a text file input and compare lines 1 and 2, 3 and 4, 5 and 6 ... ... until the end of the file. And to prompt if the lines are not equal.

Thank you!

awk ' NR % 2 == 1 { oddline=$0; next } oddline != $0 { print "line " NR-1 " and line " NR " are different." }' inputfile

bash

#!/bin/bash
while read A
do
    read B
    ((i+=2))
    [ "$A" != "$B" ] && echo "Lines $((i-1)) and $i are different"
done < inputfile
awk '{a=$0;b=NR;getline; if (a!=$0) printf "line %d and line %d are different", b,NR}' urfile

Another variation:

awk '{getline x; if(x!=$0) printf "lines %d and %d differ..\n",NR-1,NR}' infile

---------- Post updated at 19:19 ---------- Previous update was at 19:05 ----------

sed -n 'N;/\(..*\)\n\1/!{s/$/\nThe two lines above differ/p}' infile

could frans or someone else explain his code? ... I am curious to learn some scripting language but I am very poor on it :frowning: but I can see that most of you use awk (although frans used bash)... I am curious about this as well! thanks guys

while read A # read line and put in variable A. If unreadable (EOF) the break the loop.
do
    read B # read next line and put in variable B
    ((i+=2)) # increment variable i by 2 (like i=i+2)
    # [ "$A" != "$B" ] && echo "Lines $((i-1)) and $i are different"
    # can be written
    if [ "$A" != "$B" ] # if values of variables A and B are different ('[ ]' stands for 'test')
    then echo "Lines $((i-1)) and $i are different" # OK ?
    fi
done < inputfile # tells the loop to read from inputfile (else, it would read from keyboard.)

dear frans, could you please suggest me where I should start to be able to write short scripts? I knew a bit of C (pointers and structures included)... could you suggest me some workflow or even books etc. thank you in advance

If you want to learn AWK, this will get you started, and The GNU Awk User's Guide will teach you everything about AWK, it might not be really everything, thorough enough though.

You can find here : The Linux Documentation Project: Guides a couple of guides in html, pdf ...

thank you both!