Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 '13 at 0:16
    
Still, the fact that you can do that is good to know :-) –  ktm5124 Mar 12 '13 at 0:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.