LinuxQuestions.org
Review your favorite Linux distribution.
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 03-01-2005, 03:33 PM   #1
ojschubert
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Rep: Reputation: 0
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
 
Old 03-01-2005, 03:39 PM   #2
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
How do you want to change them? Can you give a before and after example?
 
Old 03-01-2005, 03:46 PM   #3
ojschubert
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 03-01-2005, 03:50 PM   #4
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
sed -i 's/39$/85/' filename
 
Old 03-01-2005, 03:51 PM   #5
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
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.
 
Old 03-01-2005, 04:12 PM   #6
ojschubert
LQ Newbie
 
Registered: Jan 2005
Posts: 8

Original Poster
Rep: Reputation: 0
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?
 
Old 03-01-2005, 04:19 PM   #7
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
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.
 
Old 03-01-2005, 05:18 PM   #8
ojschubert
LQ Newbie
 
Registered: Jan 2005
Posts: 8

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


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
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Sed q? ky-lab_rat Linux - Software 5 10-26-2004 07:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Help with sed odious1 Linux - General 2 08-29-2003 10:52 AM
help with sed DavidPhillips Programming 3 08-11-2003 04:46 PM

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

All times are GMT -5. The time now is 06:35 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