-   Programming (
-   -   A question on 'sed' (

Barbarian 04-10-2002 11:21 PM

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?

isajera 04-11-2002 12:46 AM

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


for the expressions, but you need to know what it is you're going to replace.

Barbarian 04-11-2002 01:09 AM

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!!

Mik 04-11-2002 07:35 AM

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.

Malicious 04-11-2002 10:19 PM

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.


sed ab.c << EOF
#end of script

All times are GMT -5. The time now is 08:06 AM.