LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2008, 12:07 AM   #1
netmaster
LQ Newbie
 
Registered: Oct 2006
Posts: 2

Rep: Reputation: 0
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.
 
Old 03-08-2008, 04:09 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by netmaster View Post
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?
Sed in place ("-i")?


Quote:
Originally Posted by netmaster View Post
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?
Could, yes.
 
Old 03-08-2008, 05:01 AM   #3
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
homework? Looks like it. Nice that you directly propose your work, but please remind yourself that questions concerning excercises must be marked, this is no "we do your excercises" board

now by tip:

you could use awk/sed to go through the file, use a line pattern that matches on every line and and print "the original line" if it is not the one of interest, else print the modified new line.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation in bash dalmat Linux - Software 4 12-03-2007 05:50 PM
bash string manipulation kpachopoulos Programming 3 06-17-2007 06:45 PM
string manipulation in BASH ovince Programming 4 04-16-2007 07:15 PM
String manipulation with BASH script King V Programming 9 04-21-2006 03:15 PM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration