Processing small database which is not relational


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Processing small database which is not relational
# 1  
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..
# 2  
Old 11-25-2009
Hi,

I would try something like:

Code:
tr '[:lower:]' '[:upper:]' < $opfile |
while IFS=: read cmd arg1 arg2 arg3 arg4; do
    case $cmd in
        ADD)  add [ rest of line ]
            ;;
        DELETE) delete [ rest of line ]
            ;;
        DISPLAYALL) displayall 
            ;;
        ETC)
            ;;
        *) echo "operation unkown"
            ;;
    esac
done

# 3  
Old 11-25-2009
Quote:
Originally Posted by Scrutinizer
Hi,

I would try something like:

Code:
tr '[:lower:]' '[:upper:]' < $opfile |
while IFS=: read cmd arg1 arg2 arg3 arg4; do
    case $cmd in
        ADD)  add [ rest of line ]

so is that line IFS(var?) delimiting ':' what about displayall?

also i already convert to uppcase would it be better doing it your way?
Code:
touch ../tmp/temp
tr '[:lower:]' '[:upper:]' < $opfile > ../tmp/temp
tr '[:lower:]' '[:upper:]' < ../tmp/temp > $opfile

# 4  
Old 11-25-2009
I don't have a lot of time but awk would work well here.

Something like
for i in $input
do
action=`gawk -F: '{print $1}' $i`
case $action in
ADD) grep -ov /"ADD"/"DELETE"/"DISPLAYALL"/"FINDPRODUCT"/"FINDBRAND"/ $i | add
;;
*) echo "unknown switch"
;;
esac
done

You could add the other variables to other output variables
# 5  
Old 11-25-2009
I did it like this since there is no need to tr to a file first as it can be piped straight into the loop. Indeed IFS is set to : so in the case of displayall there is only one field, so $cmd will contain "DISPLAYALL" while all the other variables will be empty.

---------- Post updated at 09:09 ---------- Previous update was at 08:45 ----------

PS. if $opfile already contains uppercase characters then you can feed it into the loop like this
Code:
while ...; do
...
done<$opfile

# 6  
Old 11-25-2009
awesome thanks guys, so long as I was on the right track in my logic... Now I just goto read 101 grep / regex tutorials within 4 days and I should be right :P
Your example, cuts down on some code and makes sense to simply add TR in pipe. so case the orginal loop, when calling my functions i just use arg1 -> 4 as variable names, and that wtf IFS is will know how to split it up like so ?
Code:
tr '[:lower:]' '[:upper:]' < $opfile |
while IFS=: read cmd arg1 arg2 arg3; do
    case $cmd in
        ADD)  add [ arg1 arg2 arg3 ]
            ;;
        DELETE) delete [ arg1 arg2 arg3 ]
            ;;
        FINDBRAND) fbrand [ arg1 ]
            ;;
        FINDPRODUCT) fproduct [ arg1 ]
            ;;
        DISPLAYALL) displayall 
            ;;
        *) echo "operation unkown"
            ;;
    esac
done

is IFS just a var name? because I can't find it in builtins or man.

Last edited by gcampton; 11-25-2009 at 05:41 AM..
# 7  
Old 11-25-2009
Almost correct. Yyou need to refer to the variable as $arg1 $arg2 etc, the first argument after cmd is called $arg1, not $arg2 and the square brackets can not be used like that but perhaps you meant it as a sort of pseudo code notation. You can of course add more variables after the read statement if need be, I do not know the exact syntax of your commands. The add, delete commands etcetera, you could define as function for instance or you could use the necessary commands in place inside the case statement.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. 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

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