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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-01-2005, 03:33 PM
|
#1
|
LQ Newbie
Registered: Jan 2005
Posts: 8
Rep:
|
help with sed
Hi,
I was wondering if someone could help get me started with sed.
I have a text document I'd like to edit. It basically looks like this:
row1: 04
row2: 93
row3: 40
I would like to change these numbers but not sure how to implement the sed command to do so.
Any help is appreciated
OJ
|
|
|
03-01-2005, 03:39 PM
|
#2
|
Member
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159
Rep:
|
How do you want to change them? Can you give a before and after example?
|
|
|
03-01-2005, 03:46 PM
|
#3
|
LQ Newbie
Registered: Jan 2005
Posts: 8
Original Poster
Rep:
|
I just want to change those values. So for example:
Before edit
row1: 20
row2: 39
row3: 34
After edit
row1: 20
row2: 85
row3: 34
Basically I just want to change the numbers ( or are they charactesr in this case?) at the end of a line.
|
|
|
03-01-2005, 03:50 PM
|
#4
|
Member
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159
Rep:
|
sed -i 's/39$/85/' filename
|
|
|
03-01-2005, 03:51 PM
|
#5
|
Member
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159
Rep:
|
the '-i' parameter tells sed to edit the file in place. The 's' is the search/replace command, the $ means "match end of line", so '39$' is a match for 39 followed by a newline character. The second pair in // (/85/ in this case) is what to replace it with.
Last edited by dsegel; 03-01-2005 at 03:52 PM.
|
|
|
03-01-2005, 04:12 PM
|
#6
|
LQ Newbie
Registered: Jan 2005
Posts: 8
Original Poster
Rep:
|
Cool, that seems easy enough. I have a couple of questions though.
What would I do if I only wanted to change the value of at the end row2: no matter what value it has? (ie. I don't need to find a specific number, i just need to update a value at the end of a row. Is there a way I can just go to that location and ovewrite whatever is there? Maybe using grep and gawk?
And with the command you just gave me, will I run into problems if there are multiple entries of the same number? Say if I wanted to change row2 from a 39 to 84 but row1 has a 39 as well.
Do these questions make sense?
|
|
|
03-01-2005, 04:19 PM
|
#7
|
Member
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159
Rep:
|
1. Do you mean you want to make updates based on row number and not the contents of the row? That's a little uglier but certainly possible, using head, tail, and grep maybe.
2. The command I listed will change all occurances that match the pattern specified. If you only want to change certain lines you need a way to specify only those lines. Is there anything else in the line that uniquely identifies the ones you want to change?
Post a more complete description/example of what you're trying to do.
|
|
|
03-01-2005, 05:18 PM
|
#8
|
LQ Newbie
Registered: Jan 2005
Posts: 8
Original Poster
Rep:
|
1. Yes I want to make editions based on a row number and not the content.
2. Yes, I only want to change a specific line. The only thing specific to the line is the label "row#:"
Basically I have a c program which will calculate a number. This number is to be writen to the text file. And there is a specific spot for this result. This document is then copied to other computers. So the other computers know the result of a certain operation. To extract a certain number I use the grep and gawk commands. I think this is the command:
grep row2: filename.txt | gawk '{print$2}'
This gets the line for row2: and gets whatever is in the second column.
Now I just need a way to write something to a specific spot in the text document.
I figure using Linux commands will be much easier than what would have to be done in the C program.
Hope this helps clarify everything
OJ
|
|
|
All times are GMT -5. The time now is 06:21 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|