Retreiving information from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retreiving information from a text file
# 1  
Old 01-06-2010
Retreiving information from a text file

Hey guys, i just started shell programming and i have a question. I am working on a simple inventory project and one of the problems which i am unable to solve would be how i am going to extract the data i need from a text file(which will be my database) and then put it inside the executable to be edited or displayed if needed. After it is edited, it needs to be stored in the same file again.

For example, in my text file, which is suppose to be my database , the information will be like this

Name : Price : Quantity
Persia : 30 :40
Shall : 26 : 31

Does anybody know how i code to extract this information from the file(database.txt) and then apply it in the shell script to be displayed , edited or deleted? if there are changes , it has to be saved back into the same file. Hope somebody can enlighten me on this. Thanks.

Last edited by gregarion; 01-06-2010 at 04:01 PM..
# 2  
Old 01-06-2010
we all know how. Smilie but you should give it a shot and then ask why your script doesn't work. here's a hint, to start you off.

Code:
grep "^Persia :" my_data_file | read name junk price more_junk quantity

# 3  
Old 01-07-2010
hmm, how about grep "persia" data_file_name ? would not this be enough to pull out the data? how come there is a need for a read command?
# 4  
Old 01-07-2010
Quote:
Originally Posted by gregarion
hmm, how about grep "persia" data_file_name ? would not this be enough to pull out the data? how come there is a need for a read command?
It is enough to pull out the data but then you need to put it into variables, which can be done through the read statement.
# 5  
Old 01-07-2010
Okay, i am having problems understanding this part. For example, if i were to a new list if items, how do i store it into variables?

Code:
function press_1
{
Number=
echo -n "Title : "
read number
if grep -q "$number" Hello1  ; then
echo "Book Exist"
fi
}

From here on, i was thinking of using an else statement , and then using > to store it back into the Hello1 file , which is the database file. but i think i would have a problem later on when i try to continue storing information about the new book's price and quantity. how do i go about with this problem? i know it has something to do with storing it into variables , but how do i get it to save the information inside the database file in order?
# 6  
Old 01-11-2010
hmmm... in all fairness, I think you may need to review chapter 1 again.

but here's some help:

Code:
function press_1
{
### ksh variables are case sensitive.
### prompt should match variable name or vice versa
### start practicing useful naming conventions.

number=
echo -n "Number: "
read number
if grep -q "$number" data_file  ; then
  echo "Book Exists"
  return
fi

### append and sort information and put back in file
(
echo $number
cat data_file
) | sort > temp_file
/bin/mv temp_file data_file
  
return
}


Last edited by quirkasaurus; 01-11-2010 at 09:21 AM.. Reason: embellishments
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inserting information from old text file into a new text file

Hello, I'm trying to take information from a list of hundreds of subject ids (where each line is a new subject id), and insert each line into a new text file that contains the pathnames for each subject. To clarify, all subject have a similiar path name (e.g., C:\data\SUBJECT_ID\) that contains... (4 Replies)
Discussion started by: invisibledwarf
4 Replies

2. Shell Programming and Scripting

Matching and retreiving numbers

Hi, I have one file 1.txt with one field consist of following Ids (shortlisted 10 but showing 3 here): 00052 00184 00607 and then second file 2.txt with three fields (very big file): 00052 00184 12.73062 00052 00598 13.51205 00052 00599 13.92554 00052 00600 13.73358... (2 Replies)
Discussion started by: bioinfo
2 Replies

3. Shell Programming and Scripting

get specific information from text file or command output

Hello, I would need some help, :wall: on a linux script, I am not sure how can I separate some text file, Text file contains something similar to this: share "userhome_e" "/fs1_100g/FILE58/userhome" umask=022 maxusr=4294967295 netbios=FILE58 share "bu share"... (3 Replies)
Discussion started by: nakaedu
3 Replies

4. UNIX for Dummies Questions & Answers

Help with retreiving files' information stored in different directories.

Dear All, I have a question. I have similarly named files stored in different directories and I'd like to retreive information from them remotely to perform other tasks. For example given: Folder 5 has files S5_SK1.chr01 S5_SK1.chr02 ....... Folder 6 has files S6_SK1.chr01 ... (3 Replies)
Discussion started by: pawannoel
3 Replies

5. Shell Programming and Scripting

get some information from a text string

deleted (3 Replies)
Discussion started by: roybean
3 Replies

6. UNIX for Dummies Questions & Answers

retreiving file name in a variable on ftp

Hi , I need to retreive the file name in an variable on the ftp server.Can you guy please let me know how can i do that.I need some thing like connect to ftp <<eof new_variable=`ls *_1_AUDIT.txt *_2_AUDIT.txt` (but on a ftp server) mget $new_variable >>eof (1 Reply)
Discussion started by: Param0073
1 Replies

7. Shell Programming and Scripting

Trying to Parse Version Information from Text File

I have a file name version.properties with the following data: major.version=14 minor.version=234 I'm trying to write a grep expression to only put "14" to stdout. The following is not working. grep "major.version=(+)" version.properties What am I doing wrong? (6 Replies)
Discussion started by: obfunkhouser
6 Replies

8. UNIX for Dummies Questions & Answers

Retreiving and storing date...

First of all want to apologize for such a simple question. Very "new" to UNIX and have just taken a small intro class. I need to pull back YYYYMMDD and store it in a field to be used later. I figured out date "+%Y%m%d" returns the date in that format, just not sure how to store it. I am... (7 Replies)
Discussion started by: cards0622
7 Replies

9. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies

10. UNIX for Dummies Questions & Answers

Extracting information from text fields.

Dear friends, I'm a novice Unix user and I'm trying to learn the ropes. I have a big task I have to accomplish and I'm convinced Unix can get the job done, I just haven't figured out how. I recently posted on the topic of cutting text between unique text patterns and somebody helped me a great... (24 Replies)
Discussion started by: spindoctor
24 Replies
Login or Register to Ask a Question