I have two files that I want to compare. A sorted and an unsorted one.
ex fileA (sorted)
ABA
FRE
DIR
ex fileB (unsorted)
AJGHEKSLANVJJFABAKEIFJA
OPTOEKSMKVMGKVABAALKKSK
is there a way to find which words from fileA exist in fileB?
I have two files that I want to compare. A sorted and an unsorted one.
ex fileA (sorted)
ABA
FRE
DIR
ex fileB (unsorted)
AJGHEKSLANVJJFABAKEIFJA
OPTOEKSMKVMGKVABAALKKSK
is there a way to find which words from fileA exist in fileB?
There may be tools to do it faster, but you could consume the first file in a loop and check like
while read -r pat; do
if grep -q "$pat" fileB; then
printf '%s has a match' "$pat"
fi
done < fileA
ABA
and not either of the other 2. It will, as written, treat your words in fileA
as patterns, not fixed strings. add -F
to the grep
command to fix that if you want. The printf
also doesn't include a newline, so that could be a mistake as well
– Eric Renouf
Feb 23 '17 at 18:35
Try this :
grep -f fileB fileA
All the lines from fileA that are there in fileB would be displayed on the console.
ABA
appears in the middle of the second line, so is a word from fileA that's in fileB, but your search wouldn't report it
– Eric Renouf
Feb 23 '17 at 15:18
-x
and -w
options could tackle this problem.
– FelixJN
Feb 23 '17 at 16:16
-x
or -w
will help with this particular problem. don_cristi's answer that he linked to above has a good potential solution: grep -oFf fileA fileB | sort -u
– Eric Renouf
Feb 23 '17 at 17:19
-w
option, note the spaces) or lines that are "A" (and this only, -x
option), respectively, WITHOUT matching lines like "thisAthat". But it looks like I misunderstood the requirements, when the questioner was referring to "words" that needed matching.
– FelixJN
Feb 24 '17 at 09:45
#
signs part of the file? – Eric Renouf Feb 23 '17 at 15:08