Issue altering end data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue altering end data
# 1  
Old 05-10-2006
Question Issue altering end data

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
# 2  
Old 05-10-2006
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

Replace line2 with line99 and it becomes:
Code:
line1
line99line3

Because line99 is one character longer, 9 overwrites the newline character. To do this properly you have to insert more characters into the middle. Or, if the new line is shorter, delete characters from the middle. This makes it a real pain.

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  
Old 05-10-2006
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  
Old 05-10-2006
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

and reading a specific record would be like
Code:
DATA=`dd bs=recordsize seek=recordnum if=recordfile`

So you'll have to use other means to find out the record number.

Be aware that dd is not a bash builtin, running dd means creating a process.

Last edited by Corona688; 05-10-2006 at 12:02 PM..
# 5  
Old 05-10-2006
I like to think of myself as neat also Smilie
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  
Old 05-11-2006
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";

but BASH is not a good language to build a relational database in. Bash The Database is a BASH extension that seems closer to what you want but is designed for connecting to remote databases, not local file-based ones. The BASH shell is just not designed for record-based file access, and neither are most scripting languages. Can you use other languages or do you absolutely need to use BASH for some reason?

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 11:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficiently altering and merging files in perl

I have two files fileA HEADER LINE A CommentLine A Content A .... .... .... TAILER AfileB HEADER LINE B CommentLine B Content B .... .... .... TAILER BI want to merge these two files as HEADER LINE A CommentLine A Content A (4 Replies)
Discussion started by: sam05121988
4 Replies

2. Programming

Altering a jar file

I have a script I am trying to test and run but it runs against a jar file. I wrote an external property file so it would redirect with my script, but it keeps going in search of the previous property file. Is there any way to externally over write the jar file and if not how do you go about... (7 Replies)
Discussion started by: risarose87
7 Replies

3. Shell Programming and Scripting

Altering a variable

Can I take an argument input, lets say it's, hg0000_xy1_v2, in the script it becomes f ... then hack off the end of the filename to change the variable to hg0000 only. I tried using sed but can't figure it out. f="$f" | sed 's/_fg_v//' I could change the variable label if necessary to... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

append | to the end of each data in a file

I have a file which has data in the below format: 7810902|6783014102| || |0| |0| |0| |0|||||T|04/13/2006||9423|7421||100|2006-04-13 16:50:28|||2006-04-13 16:50:28|n|51|-1||214 1089929|||||NewSpCreateAction request successful. Activity ID = <826528>||||100|n|2006-04-13 16:50:27|2006-04-13... (3 Replies)
Discussion started by: ankianand88
3 Replies

5. Shell Programming and Scripting

Appending data to the end of a line

I have searched the forms and I can not find info on appending each line of one file to the same line of another file. I know that I can cat one file to another or append the 2nd file to the end of the 1st but not quite sure how to append one line of data to another. For example File 1 has ... (2 Replies)
Discussion started by: scw132
2 Replies

6. UNIX for Dummies Questions & Answers

Enter data at end of a file

Hi All, I have a sample datafile: 5.1 5.2 0.8 6.1 What I want to do is create an additional 3 rows with the number "0.7". so that I now have: 5.1 5.2 0.8 6.1 0.7 0.7 (3 Replies)
Discussion started by: tintin72
3 Replies

7. Shell Programming and Scripting

Scripting question: Altering 2 field.

Hi Experts, I want to alter two filed of my data file: The _new should come to 2nd column, and _new to be removed from 4rth column, please advise, datafile.txt aa /dev/vgAA/lvol1 bb /dev/vgAA_new/lvol1 aa /dev/vgAA1/lvol2 bb /dev/vgAA1_new/lvol2 aa /dev/vgAC/lvol1 bb... (5 Replies)
Discussion started by: rveri
5 Replies

8. Shell Programming and Scripting

sed issue with end of line

Example, Trying to replace text to the end of a line. Text file looks like this PP= 4 PP= 412 PP= 425 I want to replace only the following line: PP= 4 with PP= 2 How can this be done with sed? (3 Replies)
Discussion started by: hanson397
3 Replies

9. Shell Programming and Scripting

end of line issue

Hello, I am getting few text files with no EOF ( or end-of-line ) which i need to fix using a command so that i can include it in a script. Now i'm fixing this issue by opening the file in "vi" editor and adding last line. e.g., server1#wc -l temp.txt 9 temp.txt server1#cat ... (6 Replies)
Discussion started by: prvnrk
6 Replies

10. Shell Programming and Scripting

altering numbers in files

I want to change a number in a file into number -1.. for instance file_input is fdisdlf_s35 fdjsk_s27 fsdf_s42 jkljllljkkl_s57 ... etc now i want the output to be fdisdlf_s34 fdjsk_s26 fdsf_s41 jkljllljkkl_s56 ... etc I was think of using "sed -e 's/2/1/g' -e 's/3/2/g' -e... (4 Replies)
Discussion started by: bigboizvince
4 Replies
Login or Register to Ask a Question