tags:

views:

146

answers:

3

How can I get the following output using linux command by comparing two text files? Thanks.

file1:

    site110
    site120

file2(Updated):

    domain1.com - site110
    domain2.com - site111
    domain3.com - site112
    domain4.com - site113
    domain5.com - site120
    domain6.com - site1201
    domain7.com - site1202

output:

    domain1.com - site110
    domain5.com - site120

If I use:

    grep -f file1 file2

the output will be:

    domain1.com - site110
    domain5.com - site120
    domain6.com - site1201
    domain7.com - site1202

which the last two lines are not what I want. Thanks.

A: 

How about diff?

modz0r
At first, I thought diff may solve my problem, but I don't know which option to use. Thanks.
garconcn
+3  A: 

From the grep manpage:

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

Therefore:

grep -f file1 file2

domain1.com - site110
domain5.com - site120
ire_and_curses
I tried grep -f file1 file2 before, but it didn't work on my script. I thought it doesn't work. Now, I have fixed my script. Thank you.
garconcn
@garconcn - no problem. Welcome to Stack Overflow!
ire_and_curses
A: 

May be man paste ? Some output processing can be needed.

osgx