Skip to content
Advertisement

How can i display the first five lines that differ between 2 files, in a shell script?

I ve tried using 2 arrays to compare the 2 files, but i m a rookie and i don’t know how to do it:

JavaScript

Advertisement

Answer

Do not use backticks `. Use $(..) instead.

So instead of if ( "$i" == "$j" ) do if [ "$i" != "$j" ].

vec_fis_1 and vec_fis_2 are not arrays – ${vec_fis_1[@]} is just same as $vec_fis_1.

To read a file into an array, use readarray.

To iterate over lines in file use a while read loop, see bashfaq how to read a file line by line.

JavaScript

i only need the differences between lines with the same “line index”

So read from both files at the same time.

JavaScript

You could handle a different count of lines too. I have this (rather verbose) idea:

JavaScript

or like:

JavaScript

Note that bash loops are very slow, it will be magnitudes faster with awk:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement