LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2009, 10:14 AM   #1
incomingid
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Rep: Reputation: 0
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.

Last edited by incomingid; 04-17-2009 at 10:18 AM.
 
Old 04-17-2009, 10:25 AM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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
 
Old 04-17-2009, 11:18 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 04-17-2009, 11:37 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by frieza View Post
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?
 
Old 04-17-2009, 12:47 PM   #5
incomingid
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
I have an awk command that does it, but I want to inculde sed too.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ask confirmation for every change performed by Sed script raimizou Programming 3 04-03-2009 05:03 AM
Using sed to change everything apart from one string deathalele Programming 2 03-22-2009 12:55 PM
sed to change some values in a file permanently tanveer Linux - General 2 03-23-2008 01:17 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
Sed - change some thing but not all mijohnst Linux - Newbie 5 09-01-2007 04:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration