LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2002, 11:21 PM   #1
Barbarian
Member
 
Registered: Nov 2001
Location: Malaysia and Manchester
Distribution: Red Hat
Posts: 46

Rep: Reputation: 15
Question A question on 'sed'


Using sed (or any other tool if better), how do I replace line <x> in file <y> with another piece of text without actually knowing what the original piece of text is?
 
Old 04-11-2002, 12:46 AM   #2
isajera
Senior Member
 
Registered: Jun 2001
Posts: 1,635

Rep: Reputation: 45
well... you need some form of reference - otherwise you're just going to be replacing text at random - it's going to be

s/exp1/exp2

for the expressions, but you need to know what it is you're going to replace.
 
Old 04-11-2002, 01:09 AM   #3
Barbarian
Member
 
Registered: Nov 2001
Location: Malaysia and Manchester
Distribution: Red Hat
Posts: 46

Original Poster
Rep: Reputation: 15
Ok, so I worded badly.

It is more a case of replacing the whole of line <x> with some other piece of text, hense why I want the line number.

To save on some work I wondered that if sed was streaming from a source file and then out to another it would be more than happy to replace a line of text as it went, but I get your point and my question does sound dumb - I need more coffee!!
 
Old 04-11-2002, 07:35 AM   #4
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
As far as I know sed handles everything in a stream so it doesn't keep track of the line numbers. I would use awk for something like that, since it processes things line by line. To replace line 5 in the file testfile with the text "alternatetext" you would do something like:

cat testfile | awk {'if (NR == 5) {print "alternatetext"} else {print $0}'}

I'm sure you can manage to write a nice script around that so it will work properly in your case. That example dumps the file to standard out but you can redirect it to a temp file and then replace the original file with the temp file.
 
Old 04-11-2002, 10:19 PM   #5
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
sed is a "stream" editor. The old standby line editor is "ed". Example, change line 7 in file ab.c to "trash" using a script.

Code:
#!/bin/bash
sed ab.c << EOF
7d
6a
trash
.
q
EOF
#end of script
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 question ckoniecny Linux - General 3 11-11-2005 09:31 AM
question on sed tifu Linux - Newbie 3 03-18-2005 04:38 PM
sed question virendratp Programming 3 09-10-2004 03:21 AM
little sed question freelinuxcpp Linux - Software 3 01-20-2004 07:36 AM
sed question zoomzoom Linux - Software 2 10-20-2003 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 PM.

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