Sponsored Content
Top Forums Shell Programming and Scripting Processing small database which is not relational Post 302374337 by gcampton on Tuesday 24th of November 2009 09:07:53 PM
Old 11-24-2009
Processing small database which is not relational

Hello,
Decided to edit the whole post as nobody was replying, and it was pretty darn big as it was. So I have to write this script for my assignment, and I am new to scripting. The problem is we have to handle command line args, process an operations file which adds supermarket items to a database file (by database I don't mean sql, it's simply a regular file), and send standard output. So far I have dealt with the cmdline args. But I am stuck on how I go about processing the file.
basically the input looks like this.
Code:
displayall
DEleTe:MARgarINe - canola:mEADowlea
findbrand:own brand
findbrand:Country LIFE
findproduct:Milk - chocolate
findit:bread
aDd:margarine - original:devondale:5
ADd:margarine - original:devondale:5

This will be converted to uppercase as I have already done using tr.
then I need to use grep or something to check the first word delimited by ':' or newline, eg. displayall is one command that can be used to show the whole database, add to add the rest of the line to database, delete to delete a line. etc

I'm not sure how to go about this, I'm guessing I could use a for loop to read each line and process line by line something like:
Code:
for i in $opfile
do
# grep the line somehow
    case $i in
        ADD)  add [ rest of line ]
            ;;
        DELETE) delete [ rest of line ]
            ;;
        DISPLAYALL) displayall 
            ;;
        ETC)
            ;;
        *) echo "operation unkown"
            ;;
    esac
done

so naturally I would have functions for add, delete, displayall, findproduct, findbrand.

Q1. what's the best way to go about this?
Q2. is it possible to call functions from other files, or should I separate my functions by creating separate scripts and just call on those scripts?

Reason I ask #2 is having a big wall of code makes debugging hard, for someone new to scripting like me.

Thanks.

---------- Post updated 11-25-09 at 12:07 PM ---------- Previous update was 11-24-09 at 10:12 PM ----------



Thought maybe:
Code:
#eg. line is ADD:BREAD - WHITE:TIPTOP:4

for i in $input
do
    grep -o /"ADD"/"DELETE"/"DISPLAYALL"/"FINDPRODUCT"/"FINDBRAND"/ $i | tempvar
    case $tempvar in
        ADD) grep -ov  /"ADD"/"DELETE"/"DISPLAYALL"/"FINDPRODUCT"/"FINDBRAND"/ $i  | add
           ;;
       *) echo "unknown switch"
           ;;
    esac
done

or something... would this work?
in that grep matches ADD, in current line and sends output to tempvar.
then case in tempvar is add, so then we grep the line again inverting match the rest of line being BREAD - WHITE:TIPTOP:4 and pipe to add function

Last edited by gcampton; 11-25-2009 at 12:27 AM..
 

3 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Relational operator

Hi guys , if a=12345678 i need to check that "a" less than 8 char greater than 8 char equal to 8 char contain only numeric. I done the above with seperate if condition to check and print the proper echo statement according to the result.i.e more than 8 char/lessthan etc.. can i able... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

2. UNIX for Advanced & Expert Users

Small database that can be bundled with UNIX

Hi All, I am working on a file transfer tool between unix servers. for this i am looking for a small database which is free and compatible with all unix OS and can be bundled in a package with other scripts. Any suggestions (8 Replies)
Discussion started by: Jcpratap
8 Replies

3. Shell Programming and Scripting

How to log file processing details to database table usnig UNIX shell script?

we are getting files on daily basis.we need to process these files. i need a unix shell script where we can count 1-The no of files processed 2-No of data/record processed for each files. The script should log these details into a database table. If there is any error while file... (3 Replies)
Discussion started by: Atul kumar
3 Replies
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 02:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy