-1

I have these two files containing a list of items, where the quantity of each item is separated by a space. These lists are supposed to be already ordered and always having the same amount of items each, however I would prefer making a code that relies on the item name and not on the number of the line.

I need to have an output where only the changes are present, for example an echo for every item that has changed its associated value. I know I could use diff or meld for this, but I need a very specific output, because then I have to send a mail for every one of these changes, so I guess I should be using something like awk.

cat before.txt
Apples 3
Oranges 5
Bananas 7
Avocados 2

cat after.txt
Apples 3
Oranges 7
Bananas 7
Avocados 3

output wanted:
Oranges has changed form 5 to 7
Avocados has changed form 2 to 3 
Athafoud
  • 2,898
  • 3
  • 40
  • 58
  • 5
    What did you try? SO is not meant to be a free-coding service – Inian Sep 02 '16 at 10:47
  • 2
    Welcome to SO. Please make yourself comfortable here :) How about [\[ taking a small tour \]](http://stackoverflow.com/help)? More importantly see what makes a [mcve] here. – sjsam Sep 02 '16 at 11:12

2 Answers2

2

awk is your friend

 awk 'NR==FNR{price[$1]=$2;next}
      $1 in price{
       if(price[$1]!=$2){
        printf "%s has changed from %s to %s%s",$1,price[$1],$2,ORS
       }
    }' before.txt after.txt

Output

Oranges has changed from 5 to 7
Avocados has changed from 2 to 3

If you're new to awk consider buying [ Effective awk Programming ] by Arnold Robbins.

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • @Common man! You of all the people should know that the question itself sounds fishy! This seems to be home work question and answering it without seeing any code from the author is bad! No disrespect meant! – Inian Sep 02 '16 at 11:02
  • 2
    @Inian : But if this a first time for the op with awk, then forgiven. That is why a great reference too is provided. About next time? Iron fists ;) – sjsam Sep 02 '16 at 11:07
  • 1
    I understand your point and I am sorry if my question was disrespectful, however this is no homework stuff. Actually I wish I went to a school where this was learning matter. This is actually a project of mine to monitor SMART data from hard drives and then get warnings as soon as the parameters of wear increase. Unfortunately I don't know awk at all, I just suspected that it was what I needed and I was right. Thank you very much @sjsam for this. – Andrea Favy Sep 02 '16 at 11:41
  • @AndreaFavy : You are welcome. If the answer helps you may accept it by clicking the tick mark right beneath my votes and do have a good reference for awk, preferrably the suggested one. – sjsam Sep 02 '16 at 11:43
  • I didnt mean to use your help as "free programming service", actually I'm tring hard to learn but awk is still being very though for me :( I will now try to understand bit per bit what this code is doing so I can learn more ;) – Andrea Favy Sep 02 '16 at 11:44
  • 1
    @sjsam that script won't alert you when an item has been removed from either file. – Ed Morton Sep 02 '16 at 17:24
  • 1
    @EdMorton : That's right ! But I leave it for the op to figure it out. nbd after they read the book. :) – sjsam Sep 02 '16 at 17:33
0

Not as great as other answer but simple to understand. This is not very economic way to do this task , however I have added it as it makes things simple . Of course if performance is not really a concern.

paste before.txt after.txt | awk '$2!=$4 {print $1 " are changes from " $2 " to " $4}'
Oranges are changes from 5 to 7
Avocados are changes from 2 to 3
P....
  • 17,421
  • 2
  • 32
  • 52
  • Why use two commands when you could finish the job effectively with `awk` alone? Or if you could finish the job with `paste` alone please feel free to try it. – sjsam Sep 02 '16 at 11:15
  • 1
    I agree awk is best suited to this task alone , but this one is simple to understand. No harm in knowing alternate way. – P.... Sep 02 '16 at 11:16
  • 1
    Hmm,but elegant not always means economic. Check [\[ this \]](http://stackoverflow.com/a/9109654/1620779) answer. :) – sjsam Sep 02 '16 at 11:34
  • True and I understand the downside of using paste and awk for one task. An alternate idea hit my mind so I added this as answer. :) – P.... Sep 02 '16 at 11:37