Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Script asks to input data

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-02-2012
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 31 October 2016, 11:37 AM EDT
Posts: 93
Thanks: 40
Thanked 4 Times in 4 Posts
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 07:27 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old Unix and Linux 12-02-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 16 January 2017, 1:28 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 250
Thanked 1,281 Times in 1,203 Posts
Is this homework?
Sponsored Links
    #3  
Old Unix and Linux 12-02-2012
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 31 October 2016, 11:37 AM EDT
Posts: 93
Thanks: 40
Thanked 4 Times in 4 Posts
Quote:
Originally Posted by bipinajith View Post
Is this homework?
hi,
I was graduated fifteen years ago Linux
    #4  
Old Unix and Linux 12-02-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 16 January 2017, 1:28 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 250
Thanked 1,281 Times in 1,203 Posts
Quote:
Originally Posted by baris35 View Post
hi,
I was graduated fifteen years ago Linux
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 Unix and Linux 12-03-2012
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 31 October 2016, 11:37 AM EDT
Posts: 93
Thanks: 40
Thanked 4 Times in 4 Posts
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. Linux

regards
Boris
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 08:46 PM
Echo date variable from data input to a script iga3725 Shell Programming and Scripting 6 12-07-2010 01: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 12:54 PM
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 11:53 AM
Help with Creation of Script to Input Separators in Data Alexis Duarte Shell Programming and Scripting 8 06-10-2009 10:56 AM



All times are GMT -4. The time now is 05:48 PM.