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.
Folder1/test.txt:
example1 - A exampleA - A example2 - B exampleB - C example3 - C exampleC - D
Folder2/test_different_name.txt:
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.
Here you can do with
awk
as:we are reading file1 into an
awk
associated array calledhold
with the key as first column and value from second column, where columns delimited by a hyphen we specified withawk
's-F'-'
option until theN
umber ofR
ecords is equal withF
ileN
umber ofR
ecords - 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.