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.
^M
characters at the end of thefiles_android.txt
– Stephen Harris Dec 27 '18 at 20:33\r
at the end of each line offiles_android.txt
. – Dec 27 '18 at 20:35vim
, I see"android.txt" [dos] 1268L, 171482C
and thatdos
also exists infiles_android.txt
. Is that the case? – mahmood Dec 27 '18 at 20:36[dos]
means it's in DOS format, so has the extra^M
character. – Stephen Harris Dec 27 '18 at 20:36vim
doesn't show any^M
with blue/cyan color. – mahmood Dec 27 '18 at 20:37for
loop – glenn jackman Dec 27 '18 at 21:21android.txt
that match the entries infiles_android.txt
, I'd suggestgrep -Fcf files_android.txt android.txt
– steeldriver Dec 27 '18 at 21:44-F
and-f
options forgrep
to make your script a one-liner. – peterph Dec 27 '18 at 21:51-Fcf
returns 1256. – mahmood Dec 28 '18 at 09:04