When I grep a string in a file, it gives me a match. However, when I do it in a bash script, it doesn't work.
$ cat files_android.txt
000d07dfe5016314c98b869c19c7f986b5db57db49ac76d16a5d2f5861a35072
001c738f74acbf19e3f31c09f6017de99bf3009a0b6f889740da0302ad172472
0047423956b09dd56a8b9c917d8f3028ad32ee01efdd501afa11b0777f4c184f
$ grep 000d07dfe5016314c98b869c19c7f986b5db57db49ac76d16a5d2f5861a35072 android.txt
G:\000d07dfe5016314c98b869c19c7f986b5db57db49ac76d16a5d2f5861a35072 - a variant of Android/Gappusin.C trojan
However, with the following script that read the string from the first argument and grep that in the second argument, no match is reported.
$ cat script.sh
#!/bin/bash
FILE1=$1
FILE2=$2
counter=0
for line in $(cat $FILE1); do
echo "$line"
if grep $line $FILE2; then
counter=$((counter+1))
echo "$counter"
fi
done
echo "counter=$counter"
$ ./script.sh files_android.txt android.txt
000d07dfe5016314c98b869c19c7f986b5db57db49ac76d16a5d2f5861a35072
001c738f74acbf19e3f31c09f6017de99bf3009a0b6f889740da0302ad172472
0047423956b09dd56a8b9c917d8f3028ad32ee01efdd501afa11b0777f4c184f
counter=0
What is wrong with that?
[UPDATE]
Thanks to Stephen Harris, the root was that the files were saved in dos format. So dos2unix conversion fixes the issue.
^Mcharacters at the end of thefiles_android.txt\rat the end of each line offiles_android.txt.vim, I see"android.txt" [dos] 1268L, 171482Cand thatdosalso exists infiles_android.txt. Is that the case?[dos]means it's in DOS format, so has the extra^Mcharacter.android.txtthat match the entries infiles_android.txt, I'd suggestgrep -Fcf files_android.txt android.txt