Script asks to input data | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Script asks to input data

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-02-2012
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 22 September 2014, 5:33 PM EDT
Posts: 38
Thanks: 11
Thanked 1 Time in 1 Post
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

Code:
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

Code:
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

Code:
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

Last edited by Franklin52; 12-03-2012 at 06:27 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 12-02-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
Is this homework?
Sponsored Links
    #3  
Old 12-02-2012
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 22 September 2014, 5:33 PM EDT
Posts: 38
Thanks: 11
Thanked 1 Time in 1 Post
Quote:
Originally Posted by bipinajith View Post
Is this homework?
hi,
I was graduated fifteen years ago
    #4  
Old 12-02-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
Quote:
Originally Posted by baris35 View Post
hi,
I was graduated fifteen years ago
I'm sorry. try this code:-

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

The Following User Says Thank You to Yoda For This Useful Post:
baris35 (12-02-2012)
Sponsored Links
    #5  
Old 12-03-2012
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 22 September 2014, 5:33 PM EDT
Posts: 38
Thanks: 11
Thanked 1 Time in 1 Post
Quote:
Originally Posted by bipinajith View Post
I'm sorry. try this code:-

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


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 ----------

Quote:
Originally Posted by bipinajith View Post
I'm sorry. try this code:-

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


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

regards
Boris
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
adding data in input file if 2nd script output SUCCESS nikki1200 Shell Programming and Scripting 3 10-27-2011 07:46 PM
Echo date variable from data input to a script iga3725 Shell Programming and Scripting 6 12-07-2010 12:00 PM
Help to write a script or program to automatic execute and link input file data perl_beginner Shell Programming and Scripting 2 12-03-2010 11:54 AM
Need script to take input from file, match on it in file 2 and input data darkoth Shell Programming and Scripting 1 11-03-2009 10:53 AM
Help with Creation of Script to Input Separators in Data Alexis Duarte Shell Programming and Scripting 8 06-10-2009 09:56 AM



All times are GMT -4. The time now is 08:39 PM.