Bash file manipulation help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash file manipulation help
# 1  
Old 03-08-2008
Bash file manipulation help

Hello, I'm writing a bash script and I have a question.
Here's what I'm doing:

I have a file called inv.dat which contains the following:
Code:
B102:Action Figure - Teacher:79
B103:Bike - Purple:23
B104:Baseball:25
B105:Cricket Bat:15
B101:Action Figure - Fireman:15
B100:Flame-Thrower:25

The Bash script is named manage.bash and here is what it does:

Prompts the user for the item code number. If the Item Number doesn't exist, generate an appropriate error message and terminate your shell script.
Do not ignore case sensitivity when searching for item code number. If the item number exists, extract the item quantity, prompt the user for the amount of items to increase the value in the database.

Heres what running the script looks like:

Code:
[ username ] ./manage.bash

INVENTORY MANAGE MENU

1. Generate Inventory Report
2. Add Item Quantity

Please enter a number (1,2): 2
Enter ITEM #: B106
Does not Exist - Enter ITEM #: B101
Enter units to increase "Action Figure - Fireman": 15

Here is the portion of my script that performs the addition:
Code:
if [[ $TestNum = 2 ]]; then

        echo -n "Enter value to increase " && awk -F ":" '$1 == "'"$item"'" {print $2}' inv.dat
        read value

        awk -F ":" '$1 == "'"$item"'" {OFS = ":"; print $1,$2,'"$value"' + $3 }' inv.dat >> inv.dat
        exit

fi

The addition is performed successfully using this script, however when i use the append command, it of course will add the new value to the bottom of the inv.dat file. Is there any way to add the new value and replace the old one? The point of the script is to update the quantity of the item in the database, does anyone have an ideas on how to do this? Would a Sed command help solve this issue?

I would appreciate any suggestions, thanks.
# 2  
Old 03-08-2008
After reading the variables item and value you can update your file like this:

Code:
awk -v it=$item -v val=$value 'BEGIN{FS=OFS=":"}$1==it{$3+=val}1' inv.dat

If the output is correct you can redirect it to a file and replace the original file with it.

Regards
# 3  
Old 03-08-2008
Code:
$ cat manage.sh
#!/bin/sh

FILE="./inv.dat"

f_add () {
        echo -n "Enter ITEM # "
        read ITEM
        echo -n "Enter units to increase \"`awk -F ":" '$1=="'"$ITEM"'" {print $2}' $FILE`\" # "
        read UNITS
        awk 'BEGIN{FS=OFS=":"}$1=="'"$ITEM"'" {$3+="'"$UNITS"'"}1' $FILE > $FILE.tmp
        mv $FILE.tmp $FILE
}

f_gen () {
echo "Not yet implemented"
}

while : # Loop forever
do
cat << !

INVENTORY MANAGE MENU

1. Generate Inventory Report
2. Add Item Quantity
3. Quit

!
echo -n "Your choice? : "
read choice

case $choice in
        1) f_gen ;;
        2) f_add ;;
        3) clear; exit ;;
        *) echo; echo "\"$choice\" is not a valid option."; sleep 2 ;;
esac
done

* Pattern not found is not handled above
//Jadu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text manipulation with sed/awk in a bash script

Guys, I have a variable in a script that I want to transform to into something else Im hoping you guys can help. It doesn't have to use sed/awk but I figured these would be the simplest. DATE=20160120 I'd like to transform $DATE into "01-20-16" and move it into a new variable called... (8 Replies)
Discussion started by: dendenyc
8 Replies

2. Shell Programming and Scripting

Bash - file manipulation

I need to change a file like this: John Smith;http://www.profile1.com John Smith;http://www.profile2.com Frank Olsen;http://www.profile3.com Frank Olsen;http://www.profile4.com Into: John Smith;http://www.profile1.com;http://www.profile2.com Frank... (2 Replies)
Discussion started by: locoroco
2 Replies

3. Shell Programming and Scripting

Bash array manipulation

seeking assistance on comparing two arrays using bash: array1=(disk1, disk2, disk3, disk5, disk7, vol1, vol2, vol3, vol4, vol5) array2=(disk2, disk5 vol2, vol4 ) 1) if two arrays have same elements; EXIT else populate array3 & array4 with elements that are different between array1 & array2 as:... (3 Replies)
Discussion started by: solaix14
3 Replies

4. Shell Programming and Scripting

Bash string manipulation

In my script I'm retrieving a parameter through an API call. I need to trim some things out of the result so I can use it as a parameter to pass to another process. I've got it working but it's pretty kludgy and I'm hoping someone can help me with a better way. This is the code that retrieves... (2 Replies)
Discussion started by: withanh
2 Replies

5. Shell Programming and Scripting

bash string manipulation

Hello guys, here is my problem: I got a shell script which is called by an external piece of software, the external software is not under my control. The software passes data as an argument to my script like ./bla.sh 'service;1234567890;ok;hostname;some text here' I need to pass the... (3 Replies)
Discussion started by: snoogie
3 Replies

6. Shell Programming and Scripting

Difficult problem: Complex text file manipulation in bash script.

I don't know if this is a big issue or not, but I'm having difficulties. I apoligize for the upcoming essay :o. I'm writing a script, similar to a paint program that edits images, but in the form of ANSI block characters. The program so far is working. I managed to save the image into a file,... (14 Replies)
Discussion started by: tinman47
14 Replies

7. UNIX for Advanced & Expert Users

string manipulation in bash shell

Hi All, I am using a bash shell and want to the following thing. A process sends the following string to my script BACKUP_FAIL_REASON="Failed - Application Dump CDMACA-0:grep: /opt/nortel/ca/data/1245184/sd00/image1/S110907070708HIS... (4 Replies)
Discussion started by: Pkumar Sachin
4 Replies

8. Shell Programming and Scripting

AWK manipulation in bash script

EDIT: This has been SOLVED. Thanks! Greetings everyone, I've posted a few threads with some quick help questions, and this is another one of those. I can't post enough gratitude for those much more knowledgeable than myself who are willing to give good advice for my minor issues. Now,... (2 Replies)
Discussion started by: Eblue562
2 Replies

9. Shell Programming and Scripting

bash, help with stdout manipulation.

Hey all, Im kind of lost on how to do what I want so I figured I would ask. I want to pipe STDOUT of an app to a log file, but I want to prepend each line of that output with the date and time. Im drawing a complete blank on how to do this?? Any ideas? i.e. output is currently this:... (9 Replies)
Discussion started by: trey85stang
9 Replies

10. Shell Programming and Scripting

Bash string variable manipulation

In a bash script I've set a variable that is the directory name of where an executable lives. the_dir=`dirname $which myscript` which equates to something like "/path/to/dir/bin" I need to cut that down to remove the "bin" so I now have "/path/to/dir/". This sounds easy but as a... (2 Replies)
Discussion started by: Witty
2 Replies
Login or Register to Ask a Question