A problem with "grep" on a script

Hello every one,

I have two files : file1 and file2

  • file1 contains one column and multiple lines.
  • file2 contains multiple lines and multiple columns.

I want to do a script that compare the first column of file2 with the only column of file1 then took the lines that are the same and search for each line the whole line on file2 then write the result on a file that we can call RESULT.

Here is my script :

#!/bin/bash
more file2|awk '{print $1}'|while read line
do
     grep $line file1
done|while read ligne
do
     grep '^$ligne' file2
done

But I still do not have the result that I want.

Regards,

Please post additionally an example of the input files and the expected output, thanks. Use code tags when doing so, thanks.

The line:

grep '^$ligne' file2

search the pattern $ligne and I don't think it's what you want. So it's should be

grep "^$ligne" file2

You can do

sort file1 > sorted_file1
sort file2 > sorted_file2
join sorted_file2 sorted_file1

If first column of each file are already sorted it's not necessary to sort files.

Please come up with sample data for the input and required output

Sorry I will use Code Tags on my futur posts.
Example of file1: - unix
- Solaris
- HPUX
- AIX
Example of file2: - Threads java j2ee
- Solaris sparc OS
- AIX OS IBM

the result should be: - Solaris sparc OS
- AIX OS IBM

Thank you delugeag. When I use the "..." the script works.

#!/bin/bash
more file2|awk '{print $1}'|while read line
do
grep "$line" file1
done|while read ligne
do
grep "^$ligne" file2
done>RESULTAT

Can you tell me what the difference between "..." and '...' ?

How can I do to send the file RESULT to a specific mail ? for example resultat@resultat.com ?

Regards,

man bash

       Enclosing  characters  in  single quotes preserves the literal value of
       each character within the quotes.  A single quote may not occur between
       single quotes, even when preceded by a backslash.

       Enclosing  characters  in  double quotes preserves the literal value of
       all characters within the quotes, with the exception of $, `,  \,  and,
       when  history  expansion  is enabled, !.  The characters $ and ` retain
       their special meaning within double quotes.
mailx -s "Hello world" you@youremailid.com < RESULTAT
awk 'NR==FNR{a[$1]++;next}(a[$1]){print}' file1 file2 > result.txt