I do not know if it is possible to compare the contents of text files among other files in a second folder. I need the program to only search for matching strings and if it finds them, show both lines of the files with the matching string.

I have tried with diff but I do not get the result I expect.

An example of what I'm looking for is:

Assuming I have two folders and within each one are the text files I want to compare.


example1 - A
exampleA - A
example2 - B
exampleB - C
example3 - C
exampleC - D


exampleA - A
example3 - B
exampleB - C
exampleC - D
example1 - E

The result should be the matching strings of the compared files:

I added this bit as the formatting would not fit in a comment. If this is not the desired result, please delete it from the question! CharlesGreen

|String    |File 1 |File 2 |
|example1  |A      |E      |
|example3  |C      |B      |
Note that only the matching texts are printed and when the entire line does not match, the differences are printed on the screen.
  • Does Meld is a good solution for you? It has file and directory comparison. – N0rbert Feb 10 '19 at 16:14
  • @N0rbert I just tried it and it seems that it's not what I'm looking for. When the files have different names it does not show the content differences and is not able to track if there are other files with the same content. Also, for what I need, there will be a large amount of mismatched text and it would be a task that would take a lot of time. – MarianoM Feb 10 '19 at 16:28
  • So, you are looking to find the lines in the files with the same first word, but different endings, and then show the lines from both files? – Charles Green Feb 10 '19 at 17:12
  • @αғsнιη Try again :), it's true, my objective was not clear. What I hope is that instead of looking for strings of text from a text list, we search for strings of matching texts between the files in folders 1 and 2 and if there is identical text, the result will be printed indicating the file that contains the text. – MarianoM Feb 10 '19 at 17:15
  • 1
    I added an edit to your question, if I have it wrong, please delete the edit. – Charles Green Feb 10 '19 at 17:26

Here you can do with awk as:

awk -F'-' 'NR == FNR { hold[$1]= $2; next } $2 != hold[$1] { print $1, hold[$1], $2 }' file1 file2
example3   C  B
example1   A  E

we are reading file1 into an awk associated array called hold with the key as first column and value from second column, where columns delimited by a hyphen we specified with awk's -F'-' option until the Number of Records is equal with File Number of Records - which it always true for first input file only, here file1.

then check column2 of file2 against the key with same first column we have saved in array, if those were different in second column, then we will print that.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.