Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Lets say i have two files like below:

file1

a 23
b 34
c 47

file2
a justin_beiber_sucks
b segmentation fault
c jackson
d nop

Now what i want is to get a diff of the first column of two file. I'm doing it now like

$cat file1 awk '{print $1}'> f1
$cat file2 awk '{print $1}'> f2
$diff -u f1 f2

But this approach feels retarded. Any tricky approach there?

Problem is i always view pipes as one dimensional thing, but here its two dimensional. I mean the input to diff has to come from two different sources simultaneously.

starting point may be the awk script will read both files, do filtering and save the result in a temporary associative array, next it will invoke diff and will supply its inputs.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Try:

diff -u <(awk '{print $1}' file-one) <(awk '{print $1}' file-two)

share|improve this answer
    
@DennisWilliamson, I wish I could edit comments. I am going to deleted it and re-create. –  Zoredache Jul 26 '12 at 1:18
1  
Also known as Process substitution. tldp.org/LDP/abs/html/abs-guide.html#PROCESS-SUB –  Zoredache Jul 26 '12 at 1:19
    
linking this page will be more appropriate tldp.org/LDP/abs/html/abs-guide.html#PROCESS-SUB –  Aftnix Jul 26 '12 at 9:40

Chances are good that join will do what you want:

join <(sort file1) <(sort file2)

But since you don't say what you are really trying to accomplish, it's difficult to determine what options might be appropriate.

share|improve this answer

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.