0

Suppose I have the file FirstDB.txtand it's populated. With the format productID:productName:Quantity:Price:DateRegistered

example: cat FirstDB.txt

0001:Fried Tarantula:15:100:2017-08-27
0002:Wasp Crackers:18:25:2017-08-27
0003:Century Egg:19:50:2017-08-27
0004:Haggis Flesh:20:90:2017-08-27
0005:Balut (Egg):85:15:2017-08-27

then I have a file that is generated by the user named .cart and it's populated as well with the format productID productName Quantity IndividualPrice TotalPrice

cat .cart

0001    Fried Tarantula     5   100     500
0003    Century Egg         7   50      350
0005    Balut (Egg)         6   15      90

I would like to update the FirstDB.txt product quantity with what the user purchased or selected. Considering the two files that I have above what I would like to happen after is this output:

cat FirstDB.txt

0001:Fried Tarantula:10:100:2017-08-27
0002:Wasp Crackers:18:25:2017-08-27
0003:Century Egg:12:50:2017-08-27
0004:Haggis Flesh:20:90:2017-08-27
0005:Balut (Egg):79:15:2017-08-27

I only have an idea of cutting the first column of .cart and use grep $(cat .cart) to get the lines that I need then cut the third column of .cart to obtain the quantity then subract it to the third column of FirstDB.txt however it seems like i can't get the output that I want. I'm also thinking of using sed -i on FirstDB.txt but haven't tried it yet. Any help? Thank you so much!

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • You only can use `sed`? and no other language on the system? – webdeb Sep 04 '17 at 13:59
  • Not really! Anything goes as long as it can be done in bash shell. Tho I refrain from using awk since I haven't really learned or mastered that bit yet. So to conclude we can use anything actually :) – Gifter Villanueva Sep 04 '17 at 14:04
  • is this a assignment? I find it hard that you can't use tools like sqlite to solve this problem. Take a look at SQLite. It can do this very efficiently. Is that an option? – NinjaGaiden Sep 04 '17 at 14:31
  • The whole purpose of this exercise is to learn `awk`. – William Pursell Sep 04 '17 at 14:38
  • I apologize for the confusion but the database is not actually a database but rather it's a text file. Edited the question already! This is why I can't use any other tools except for terminal commands on bash. – Gifter Villanueva Sep 04 '17 at 14:39
  • Just did! Sorry for the confusion once again. Thank you so much for taking the time to read the question anyway! – Gifter Villanueva Sep 04 '17 at 14:40
  • You're very welcome, thanks for taking the effort to improve your question :) – Aaron Sep 04 '17 at 14:41
  • 1
    Check out [this question](https://stackoverflow.com/questions/14984340/using-awk-to-process-input-from-multiple-files) if you haven't already. The idea would be to handle the `.cart` file first building an array of changes, then when you handle the `firstDB.txt` file you could check in the array to apply the changes before displaying the data ; I see you haven't listed `awk` in your tags, but while doing this with pure `bash` ought to be possible (although probably slower than `awk`), using `sed` would just bring artificial complexity – Aaron Sep 04 '17 at 14:46
  • Thank you so much for the heads up Aaron! I was just looking at the proper usage of `awk` minutes ago and the link you gave seems to be pretty helpful! I'll start looking at it right away. If I get to answer my own question i'll just post it here thank you! – Gifter Villanueva Sep 04 '17 at 14:52

1 Answers1

1

sed is for simply doing s/old/new/, that is all. You aren't simply doing s/old/new/ so you shouldn't be considering using sed. Just use awk, even if you do have to learn it now...

$ cat tst.awk
NR==FNR { a[$1] = $(NF-2); next } # save a["0001"]=5, etc from cart
FNR==1 { FS=OFS=":"; $0=$0 }      # split FirstDB by ":"s instead of spaces
{ $3 -= a[$1]; print }            # when $1=="0001" set $3 to $3-a["0001"]

$ awk -f tst.awk cart FirstDB.txt
0001:Fried Tarantula:10:100:2017-08-27
0002:Wasp Crackers:18:25:2017-08-27
0003:Century Egg:12:50:2017-08-27
0004:Haggis Flesh:20:90:2017-08-27
0005:Balut (Egg):79:15:2017-08-27
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Thanks Ed! I've been studying awk for a while now and I'm still trying to get the ropes of it. Do you mind breaking down how `tst.awk` works? Thanks! – Gifter Villanueva Sep 04 '17 at 16:12