Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

top1

1,facebook.com
2,google.com
3,youtube.com
4,yahoo.com
5,wikipedia.org
6,baidu.com
7,live.com
8,amazon.com
9,qq.com
10,twitter.com

top2

1,facebook.com
2,google.com
3,youtube.com
4,yahoo.com
5,baidu.com
6,wikipedia.org
7,live.com
8,qq.com
9,amazon.com
10,twitter.com

diff -u  /Users/stofke/Desktop/top2a.csv /Users/stofke/Desktop/top1a.csv |
  sed -n '1,2d;/^[-+]/p' |
  sort --field-separator=',' --key=2 

+9,amazon.com
-8,amazon.com
+5,baidu.com
-6,baidu.com
+8,qq.com
-9,qq.com
+6,wikipedia.org
-5,wikipedia.org

But I actually need this:

 +1, amazon.com
 -1  baidu.com
 -1  qq.com
 +1  wikipedia.org

I don't know how to do that last step

(I would also need to calculate the % wise decline or raise)

Adding | paste -d, - -

gets me this

+9,amazon.com,-8,amazon.com
+5,baidu.com,-6,baidu.com
+8,qq.com,-9,qq.com
+6,wikipedia.org,-5,wikipedia.org
share|improve this question
    
Diff doesn't do this. You'll need to write a small script that does it. –  Dennis Kaarsemaker Jan 20 '13 at 17:08

2 Answers 2

up vote 2 down vote accepted

You could also consider an approach using join since you want sorted output in the end. Like:

join -t, -j2 <(sort -t, -k2 file1) <(sort -t, -k2 file2) |
   awk -F, '{n=$3-$2; if (n) print n, $1}'
share|improve this answer
    
I like this answer, it's easy to read and does it well –  Stofke Jan 20 '13 at 21:04

Using awk

awk -F, '
    NR == FNR {n[$2] = $1; next}
    {n[$2] -= $1}
    END {for (k in n) if (n[k] != 0) printf " %2d  %s\n",n[k],k}
' top1 top2

outputs

 -1  amazon.com
  1  qq.com
 -1  wikipedia.org
  1  baidu.com

order not guaranteed.

share|improve this answer
    
I bet it's good but it's too hard for me to read it and understand it. Anyway thank you very much for this answer –  Stofke Jan 20 '13 at 21:05

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.