LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using SED to change a file (http://www.linuxquestions.org/questions/linux-newbie-8/using-sed-to-change-a-file-719857/)

incomingid 04-17-2009 10:14 AM

Using SED to change a file
 
I have a database that looks like this:

Maria:400
John:1000
Mike:350.13
Henry:70.56

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
then
#need something here to calculate $amount3 that I used below
sed "s/$name*/$name,:,$amount3/g" file

else
echo -n $name >> file
echo -n ":" >> file
echo $amount2 >> file
fi

________________________________________

Thanks.

frieza 04-17-2009 10:25 AM

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

ghostdog74 04-17-2009 11:18 AM

you can just use the shell
Code:


# prompt user here and get the user value into variable
#

while IFS=":" read a b
do
 case "$a" in
  $uservalue )) echo "do something;;
 esac
done < file


Disillusionist 04-17-2009 11:37 AM

Quote:

Originally Posted by frieza (Post 3512293)
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

I wouldn't use sed for your scenario

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?

incomingid 04-17-2009 12:47 PM

I have an awk command that does it, but I want to inculde sed too.


All times are GMT -5. The time now is 08:20 PM.