Using SED to change a file
I have a database that looks like this:
I'm gonna prompt the user to enter a name and an amount. If the Name doesn't exist then I will add it. If the name exists, then I will have to add the amount that was already there with the amount that the user entered. I am having trouble with adding the amounts togeder. I don't know how to separate the amount from the name.
This is my work so far:
(I just started using sed and I have a feeling everything is wrong in my script so far.)
if sed "/$name*/" file
#need something here to calculate $amount3 that I used below
sed "s/$name*/$name,:,$amount3/g" file
echo -n $name >> file
echo -n ":" >> file
echo $amount2 >> file
well first of all you dont use sed to search a file, that would be the job of grep
and second.. sed by default dumps to stdout, i havnt't used sed enough to know the exact command to get it to actually EDIT the file but man sed would be a starting point if you havnt already
otherwise >> file2, move file1 to an archive rename file2 to file1
you can just use the shell
sed -i filename get's it to do an "inline" edit, however that doesn't deal with your calculation requirement.
How big is the datafile that you need to work on?
For small files (1000 lines or less) Ghostdog74's suggestion would be the easiest way to go.
Are you planning to test for unique names in the file?
I have an awk command that does it, but I want to inculde sed too.
|All times are GMT -5. The time now is 02:16 PM.|