How to diff and show where is the different in a line between two files

StackOverflow https://stackoverflow.com/questions/17310149

  •  01-06-2022
  •  | 
  •  

سؤال

File a-

aaabaaa

File b-

aaaaaa

Request output sample-

aaa-aaa

*- mean there is a letter missing

How to done this using basic unix command or shellscript or sql or 4gl code? ( need just any 1 )

هل كانت مفيدة؟

المحلول

Something like this should work (file order is very important and this compares first line of first file with first line of second file):

awk '
NR==FNR {
    a[NR]=$0
    next
}
{
    delete ary
    delete ary2
    x=y=len=i=k=0
    x=split($0,ary,""); 
    y=split(a[FNR],ary2,"");
    len=x>y?x:y;
    while(len>0) {
        if (ary[++i]==ary2[++k]) {
            printf ary[i]
        }
        else
        {
            printf "-"
            i--
        }
        len--
    }
    print ""
}' file1 file2

Test:

$ cat file1
aaabaaa
abcdefabc
aaabbbccc

$ cat file2
aaaaaa
abcabc
acacacac

Output:

aaa-aaa
abc---abc
a-----c--
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top