![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| altering numbers in files | bigboizvince | Shell Programming and Scripting | 4 | 03-26-2008 07:34 AM |
| Unix Arithmatic operation issue , datatype issue | thambi | Shell Programming and Scripting | 23 | 02-19-2008 04:19 AM |
| how to verify that copied data to remote system is identical with local data. | ynilesh | Shell Programming and Scripting | 3 | 01-31-2008 05:55 AM |
| Howto capture data from rs232port andpull data into oracle database-9i automatically | boss | UNIX for Dummies Questions & Answers | 1 | 09-22-2007 11:35 PM |
| Data Extraction issue. | irehman | Shell Programming and Scripting | 5 | 04-06-2006 05:28 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
I have an inventory program that I would like to have the ability to go and change or alter the field data based on the item number as a key. I have the menu option set but at the end of the script process it just appends the changed data to the database rather than what I would like; which is to replace the current item number and fields that were changed in the menu.
Any advice would be appreciated. The inventory change script is below. #!/bin/bash #=================================================== # Script Name: invadd # By: jhaynes # Date: 4-4-2006 # Purpose: Script loop to add inventory to the HC # computing inventory list, while preventing # duplicate item#. # Command Line:invadd #=================================================== trap "rm ~/tmp/* 2> /dev/null ; exit" 0 1 2 3 invfile=~/final/hc_inventory looptest=y while [ $looptest = "y" ] do clear tput cup 25 4; ./appendinv; tput cup 1 4; echo "HC Inventory Change" tput cup 2 4; echo "================================" tput cup 4 4; echo "Item Number: " tput cup 5 4; echo "Description: " tput cup 6 4; echo "Dept : " tput cup 7 4; echo "Cost : " tput cup 8 4; echo "Qty : " tput cup 10 4; echo "Add Another? (y)es or (q)uit: " tput cup 11 4; echo "Please enter an item number or q to quit " tput cup 4 16; read itemnum if [ "$itemnum" = 'q' ] then clear ; exit fi #Check to make sure the Item number field is not blank if [ "$itemnum" = "" ] then tput cup 11 2; echo "***Item number can not be blank*** " tput cup 13 2; echo "******Hit ENTER to continue******" tput cup 4 16 ; echo " " tput cup 4 16 ; read itemnum continue fi tput cup 5 16; read descr while [ "$descr" = '-' ] do tput cup 4 16 ; echo " " tput cup 4 16 ; read itemnum if [ "$itemnum" = 'q' ] then clear ; exit fi tput cup 5 16 ; read descr done tput cup 6 16; read dept while [ "$dept" = '-' ] do tput cup 5 16 ; echo " " tput cup 5 16 ; read descr if [ "$descr" = 'q' ] then clear ; exit fi tput cup 6 16 ; read dept done tput cup 7 16 ; read cost while [ "$cost" = '-' ] do tput cup 6 16 ; echo " " tput cup 6 16 ; read dept if [ "$dept" = 'q' ] then clear ; exit fi tput cup 7 16 ; read cost done tput cup 8 16 ; read qty while [ "$qty" = '-' ] do tput cup 7 16 ; echo " " tput cup 7 16 ; read cost if [ "$cost" = 'q' ] then clear ; exit fi tput cup 8 16 ; read qty done #Check to make sure fields are not blank before you write the data4 if [ "$itemnum:$descr:$dept:$cost:$qty" > " " ] then echo "$itemnum:$descr:$dept:$cost:$qty" >> $invfile fi tput cup 10 33; read looptest if [ "$looptest" = 'q' ] then clear ; exit fi done |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Editing a line in the middle of a file is a LOT harder than adding a line to the end of a file. Adding to the end just means opening in append mode, but selectively editing a line in the middle of it means reading in the file completely, modifying the contents, then writing back the edit and everything after it.
The reason it's so hard is, what if the length of the line changes? You can't just replace the line or stuff like this happens: Code:
line1 line2 line3 Code:
line1 line99line3 If you use fixed record sizes, you'll be able to just seek to and overwrite arbitrary records. See 'man dd'. bash may not be the best language for this sort of work, however. |
|
#3
|
||||
|
||||
|
Thanks Corona688. I understand what you are saying. I am useing fixed-length records. I will look to the man dd pages. To see if there is a way to update the veriables based on the item number being the key identifier to the line.
|
|
#4
|
|||
|
|||
|
You are? Neat. I didn't catch that since I'm not familiar with all the commands you're using in your script.
dd doesn't give much consideration to what data it's copying or writing. You can't tell it what to overwrite, only where. Overwriting a specific record would be something like Code:
echo "${DATA}" | dd bs=recordsize seek=recordnum of=recordfile
Code:
DATA=`dd bs=recordsize seek=recordnum if=recordfile` Be aware that dd is not a bash builtin, running dd means creating a process. Last edited by Corona688; 05-10-2006 at 08:02 AM. |
|
#5
|
||||
|
||||
|
I like to think of myself as neat also
Here is an example of the inventory list HC Inventory List Dept Item# Description Qty Cost ==== ===== ============================== ===== ====== 0100 10001 Intel Celeron - 1.8GHz 7 $65.00 0100 10002 Intel Pentium 4 - 2.4GHz 10 $125.00 0200 10003 AMD Athlon XP 2800 12 $108.00 0100 10004 Intel Mobile Pentium 4 1.7GHz 6 $170.00 0100 10005 Intel Xeon 1.8GHz 8 $183.00 0100 10006 Intel Pentium 4 - 3.2GHz 4 $285.00 0300 10007 Kingston 256MB PC133 20 $50.39 0300 10008 Kingston 512MB RDRAM Kit 6 $240.00 So as an example, lets say I wanted to change the qty of item # 10007 other than dd, are there any other tools that might better achive what I am looking to do. |
|
#6
|
|||
|
|||
|
I forsee problems no matter what method you use. What you seem to be trying to build is a rudimentary relational database, to let you do queries like
Code:
UPDATE INVENTORY SET B="value1", C="value2", D="value3" WHERE A="12345"; Another thing I notice is that, unless you keep all the records in your database sorted by item number, there is no way to find a specific number except checking every single row. This will be very slow when your database grows larger. Keeping them ordered will let you find the record you want with a binary search. Last edited by Corona688; 05-11-2006 at 07:42 AM. |
|||
| Google The UNIX and Linux Forums |