-
Writing To Certain Lines
Hi,
Let's say if have the following text file:
Code:
Line 1
Line 2
Line 3
Line 4
...
Line 10
Is there any way I could add something "Line 10" so it said something like "This is Line 10", but nothing else? Basically what I'm asking is how to add things to certain lines instead of just appending to a file. Can anyone help me? Thanks.
BTW, if possible I would prefer fstreams (I'm not sure what else there is), but if not that's ok.
-
Since each line can be of varying length, there is no way to just go to a specific line and insert information there -- all the data following that line has to be moved in the file to make room.
Generally, line-based processing programs do their work by reading a line at a time from an input source, doing something to each line, and outputting the new lines to a new file. In other words, "in-place" editing of text files is not possible without a lot of buffering.
-
Or try perl:
Code:
perl -ip -e 's/^(.+)$/This is \1/' FILENAME