0

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 )

Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33

1 Answers1

3

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--
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • hi may i ask if there is extra line in 1 of file will it still manage to check where is the difference? example like all --- in the extra line. – user1961638 Jun 26 '13 at 03:52
  • As I mentioned that file order is very important. If extra line is present in second file then yes the above solution will work. If extra line is present in first file then we will have to add additional code. – jaypal singh Jun 26 '13 at 04:09