[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    (o) Checkout an auto part: should prompt the user for the name of the auto part
    and borrower's name:
    Name:
    Borrower's name:
    After reading the name of the auto part and the borrower's name, this option
    should verify that the auto part exists and that it is checked in. If so, the status
    should be changed in the database, otherwise an error message should be
    generated. The system date should be used to update the checkout date.

Basically I have an entry in the file that lists the parts (auto.parts)
i.e
Brakes:Frank:in::

What needs to happen is someone goes to "check out" the part, they enter the part name then the borrower's name and then the date, then I need to have it edit auto.parts to reflect the person who borrowed the part and the date they borrowed it on.

Brakes:Frank:in:0:na

turns into

Brakes:Frank:out:11/25/09:Mike

  1. Relevant commands, code, scripts, algorithms:
    Trying to use some variation of sed to do this

sed -i -e "/$copart/s/<old>/<new>/g" auto.parts

  1. The attempts at a solution (include all code and scripts):

So far I have:
...roughly...

echo "Part to be checked out:"
read copart
echo "Enter date:"
read codate
echo "Enter borrower's name:"
read bname

here is where im stuck I'm trying to use sed with something along the lines of:

sed -i -e "/$copart/s/in/out/g" -e "/$copart/s/0/$codate/g" -e "/$copart/s/na/$bname/g" auto.parts

I want it to find he line that has $copart in it then change the two fields with 0 to the date and na to borrower's name.

I know the way im changing my date isn't how it is said in the instructions but im trying to get over this first hurdle before tackling that issue.

I've been chugging at this on my own scouring the net for HOURS not having any luck finding anything (anything I'm able to comprehend at this point my brain is fried) I'm **hoping** I can get some insight here.. im getting desperate

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Georgia State University | Atlanta, GA USA | Fasheng Qiu CSc 3320

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

why ask the user to enter the date? if you're supposed to used the system's date just do

codate=`date`

i haven't run the your code but it seems correct... what's the problem, you're not able to find the relevant line in the database? have you tried using grep?

---------- Post updated at 09:34 PM ---------- Previous update was at 09:29 PM ----------

i looked at the sed command, if you're trying to find the pattern you need the p command (at least you do on my system)

/$copart/s/in/out/pg

I see two fundamental problems with your script:

  1. User input has to be validated
    You write the user has to enter the name of the part. That raises the question: what happens, if the part s/he enters is not in the database? It is ok, IMHO, if you say that this is beyond the scope of the homework or that false input can be silently ignored or whatever - it is just a fundamental truth that any input users provide should be validated somehow and you dhould have an answer to this question. If it is the correct data type (strings, digits, ...), if it is "well-formed" in some regard (for instance an IP address looks always the same - 4 numbers, 0-255, divided by 3 dots, etc.), and so on. If your program makes some assumption about which data it will be presented then check if indeed this data is what you have before you use it.

  2. The sed-script
    There are three basic forms of sed commands:

s/<expr1>/<expr2>/

/<expr>/ {
       command1
       command2
       ....
     }


/<expr1>/,/<expr2>/ {
       command1
       command2
       ....
     }

The first one is the most simple one: it applies the single command to replace <expr1> with <expr2> to every line. It might be that <expr1> is not part the line and the command will do nothing in this case, but still it is applied to the line (this is necessary to understand how sed works).

The second one is a more complex form: it will successively apply command1, then command2, etc. to lines which contain <expr1>. Of course the "list of commands" could also consist of only one command, in which case you can omit the curly braces and write:

/expr/command

An example: The following script will change only the lines that start with "abc". The change will be to first replace the "c" in "abc" to "<command1>", the second change will be to replace the line end with "<command2>", which effectively appends this to the end of the line.

sed '/^abc/ {
          s/c/<command1>/
          s/$/<command2>/
     }' in > out

file in:
========
abc will be changed
abd will not be changed
xxx next line will be changed, but this will not
abc

file out:
========
ab<command1> will be changed<command2>
abd will not be changed
xxx next line will be changed, but this will not
ab<command1><command2>

The third and last form ist the most complex one: it will apply a (list of) command(s) to a range of lines. It will start applying the list of commands when a line containing <expr1> is encountered and will do so on any subsequent lines untl <expr2> is found in a line. Then the commands are not applied to any line until <expr1> is found again, etc.. Similar to the second variant you can replace the list of commands by a single command where you can omit the curly braces:

/expr1/expr2/ommand

See if you can solve your problem now.

I hope this helps.

bakunin