Strange error: grep does not read from file

I have two files
file1.txt

angie
mary
susan

file2.txt

angie blond
mary brunnet
susan red
christine black

I want to get this output

angie blond
mary brunnet
susan red

I write

grep --file=file1.txt file2.txt

and i get no results

i also wrote

cat file1.txt|while read line file1.txt;do grep "$line" file2.txt;done

again no results

I tried

while read line file1.txt;do echo -e "$line";done

nothing

but when i write

while read line;do echo -e "$line";done<file1.txt

it reads the lines

I don't understand that, why it does not read the input files inside the code?

If i solve this i will be able to run the first command, right?

Should be:

cat file1.txt|while read line ;do grep "$line" file2.txt;done

But your last example is maybe better still...

Hi, Use grep with option -f

grep -f file1.txt file2.txt.

Hope it should work.

I tried but it does not work...

---------- Post updated at 09:38 AM ---------- Previous update was at 09:38 AM ----------

It does not work either....

Who are you kidding?

ran:/home/vbe $ cat file1.txt|while read line ;do grep "$line" file2.txt;done
angie blond
mary brunnet
susan red

for me it does not work, with the actual files i have.
That is why i wrote "strange" in the beginning. I've run it before and it worked fine
but i do not know what could be wrong now.

I created exactly the files with the given names and content...
So start to see if the cat command part works or check your perms on those files try to give var name using UPPERCASE which will prevent you to having to look for reseved words or aliases...

Just wondering...
What key stoke do you use to get the pipe?
Ctrl-1 or Alt GR-7

My mitake instead of grep use egrep

egrep -f test1 test2
angie blond
mary brunnet
susan red

I am getting the requird result.

---------- Post updated at 10:17 AM ---------- Previous update was at 10:15 AM ----------

Hi, instead of grep you have to use egrep

egrep -f test1 test2
angie blond
mary brunnet
susan red

I am getting the required result

When i type this

while read line file1.txt; do echo -e "$line";done

it does not work
when i type this

cat file1.txt|while read line; do echo -e "$line";done

it does work.

Could this be helpful?

For the other part, you mean to rename the files to uppercase letters?

---------- Post updated at 10:33 AM ---------- Previous update was at 10:31 AM ----------

Neither the egrep works

---------- Post updated at 10:33 AM ---------- Previous update was at 10:33 AM ----------

Sorry, i did not understand what you mean...

Yes first example does not work because its a non sence...
read command reads something, a VAR a line from input ( and so cant read a file, but cat does...)
I mean by uppercase e.g.

cat file1.txt|while read LINE; do echo -e "$LINE";done

Addendum...
I ment depending from where you come from and what keyboard layout you have, you may not choose the correct one... For the only reason I saw at that time for the line to not work was the | (for me here it would be AltGr+ 7 keys to depress)

You can definitely read a line from a file in a while loop:

while read LINE
do
        echo -e "$LINE"
done < file1.txt

This will also prevent side-effects like variables being set inside the loop not being seen outside it.

Corona688:
You are absolutely right but that, our member already knows and tried ( see the beginning of thread...and I also sugggested was better...) but what he could not understand what was incorrect in his example giving a

read VAR <a file>

Maybe could you explain to our guest the reason or how works the redirection ( I wonder if its not there the confusion - where in input using < you specify a file, but doesnt work with read <variable> file...)

It may sound silly but, since i want to use grep i actually want two input files, file1.txt and file2.txt

How do I do this? I mean in my case the command

cat file1.txt|while read line; do grep "$line" file2.txt;done

does not give me result. When i use the < though to set the input file it works.
So i wonder how can i do this with my two input files?
I hope i am not very confusing but i have no idea why WITH THE PARTICULAR FILES it does not work

If you look at my previous posts, you will see the output where I showed you it works

ran:/home/vbe $ ll -lt|grep file                                             
-rw-rw-rw-   1 vbe        bin             51 Sep 20 16:55 file2.txt
-rw-rw-rw-   1 vbe        bin             17 Sep 20 16:55 file1.txt
-rw-rw-rw-   1 vbe        bin             44 Nov 11  2010 filename
ran:/home/vbe $ cat file*txt
angie
mary
susan
angie blond
mary brunnet
susan red
christine black
ran:/home/vbe $ cat file1.txt|while read line ;do grep "$line" file2.txt;done
angie blond
mary brunnet
susan red
ran:/home/vbe $ 

Perhaps the files processed are not in unix file format ?
Please check that.

Regards
Peasant.

I suggest that the OP provide an unambiguous dump of his/her text file, using hexdump -C, od -bc, or whatever tool is available to give us a definitive manifest of the files' contents.

The following is an example of data that appears fine when sent to the terminal but thwarts grep's attempt to match it:

$ printf 'a\000bc\n'
abc
$ printf 'a\000bc\n' | grep abc
$

Also, it wouldn't hurt to post a trace of your shell's actions. Assuming you're using a posix-like shell, you can probably enable tracing with set -x (you can then disable it with set +x ).

Regards,
Alister

Can you please tell me how to do that?
Thank you

---------- Post updated at 03:42 AM ---------- Previous update was at 03:39 AM ----------

Maybe its better to provide my actual data to see if i find a solution
file2.txt

Les.1000.1.A1_at    BG628706
Les.1007.1.A1_at    BG628731
Les.1008.1.A1_at    BG628739
Les.1009.1.A1_at    BG628745
Les.101.1.S1_at    AJ002298.1
Les.1011.1.A1_at    BG628754
Les.1013.1.A1_at    BG628758
Les.1015.1.A1_at    BG628777
Les.1018.1.A1_at    BG628791
Les.1019.1.A1_at    BG628793
Les.102.1.S1_at    AF022874.1

file1.txt

Les.1013.1.A1_at
Les.1015.1.A1_at

---------- Post updated at 04:09 AM ---------- Previous update was at 03:42 AM ----------

There is probably something wrong with the file "encoding", originally it is an excel file from windows

I get:

ran:/home/vbe $ cat file1.txt|while read line ;do grep "$line" file2.txt;done
Les.1013.1.A1_at    BG628758
Les.1015.1.A1_at    BG628777

So you do the same: do a copy paste if you can from your post, dont edit just copy /paste straight to your unix box but can you ? (Im using ReflectionX...).