Script asks to input data

Hi,

I have three different files about a warehouse's stock status.

Each file shows storage locations, stored product names, quantity of the part and at last column, its price.
When there is a change in price, I open those files one by one, search related product name at each row and change the price manually.

What I would like to have is a a script to make the task easier: It will ask me to enter which product name I am looking for. When I enter the product name in terminal panel, it will ask me the second question, the new price. When I enter new price in same format, it will replace the old one by the new one in all three files.

Here are my sample files:

database1.txt

store1	product101	  440	�40
store2	product102	  300	�22
store2	product103	  210	�39
store2	product104	  120	�40
store1	product105	  100	�29
store1	product106	     5	�34
store3	product107	    90	�19
store3	product108	  209	�20
store4	product109	  109	�10
store4	product110	  103	�12
store5	product111	  298	�90

database2.txt

store4	product101	  40	�40
store3	product102	   9	�22
store2	product103	  91	�39
store2	product104	  12	�40
store1	product105	  10	�29
store2	product106	  5	�34
store9	product107	  90	�19
store3	product108	  29	�20
store4	product109	  19	�10
store8	product110	  13	�12
store5	product111	  28	�90

database3.txt

store1	product101	  440	�40
store2	product102	  20	�22
store2	product103	  30	�39
store6	product104	   6	�40
store6	product105    9	�29
store3	product106	   5	�34
store7	product107   90	�19
store9	product108	 209	�20
store8	product109	 109	�10
store4	product110  103	�12
store1	product111	 298	�90

I am not sure if this question should be placed under "shell script" topic.
I would appreciate if you could help me.

thanks in advance
Boris

Is this homework?

hi,
I was graduated fifteen years ago :slight_smile:

I'm sorry. try this code:-

#!/bin/ksh

echo "Enter product name: "
read pname
echo "Enter product price: "
read price
price=$( echo "\0243"$price )  # Formatting price with pound sign.

for file in database1.txt database2.txt database3.txt
do
        awk -v PN=$pname -v PR=$price ' { if($2==PN) $4=PR; print $0; } ' $file > tmp; mv tmp $file;
done
1 Like

Thanks a lot Sir,
I will test it and let you know in this page.

regards
Boris

---------- Post updated 12-03-12 at 09:03 PM ---------- Previous update was 12-02-12 at 10:42 PM ----------

Hi,
Thank you so much! That is perfect!
By running your script, I have edited tens of files in second. :slight_smile:

regards
Boris