0

I have two files, team1.txt and team2.txt

team1.txt
===========
Alex 
Bob
Charlie
David
team2.txt
===========
Alex
Charlie
Benjamin
Zed
Noah

I want to output the people who are in both team1 and team2 using a unix command.

$ cat team1.txt | xargs grep -s team2.txt -e
team2.txt:Alex

The problem is that this is incorrect. Both Alex AND Charlie are in team1 and team2. But for some reason grep stops after the first match. Why is this? xargs should be running grep on every line from team1.txt.

1 Answer 1

1

That's because your Alex in team1.txt has a trailing space, and the one in team2.txt doesn't. That said: why don't you just use

grep -f team1.txt team2.txt 
Alex
Charlie

This works (I cleaned up the extra space) well, and only requires one command invoked once.

2
  • You're right, it works now! The trailing space... The reason I can't use your command above is because the original problem wasn't as simple as the example I gave - team2.txt has a bunch of other text on each line with the name in it.
    – ktm5124
    Mar 12, 2013 at 0:16
  • Still, the fact that you can do that is good to know :-)
    – ktm5124
    Mar 12, 2013 at 0:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .