Add a line into a file at multiple locations above a existing line

Hi All,

I am working a ruby code to append lines to a existing file at some
specified location. I do that by finding a particular string and append
the line below that. In a certain case i am trying to add a particular
line say welcome before a existing line,

Eg. line1
line 2
line3
)

line4

I will need to add the line above line 4 as i cant append it after ) as
there may be several )'s. What is the way that i can do this?

It would be of great help. Thanks in advance.

The easiest way to do this is to read the file, edit it as a string, and
overwrite the file with the modified string.

On Fri, Aug 24, 2012 at 10:22 AM, Aravind K. [email protected]
wrote:

)

line4

I will need to add the line above line 4 as i cant append it after ) as
there may be several )'s. What is the way that i can do this?

It would be of great help. Thanks in advance.

Just iterate the file line by line and output the new line before you
output the matching line.

Kind regards

robert

On Fri, Aug 24, 2012 at 8:49 AM, Joel P. [email protected]
wrote:

The easiest way to do this is to read the file, edit it as a string, and
overwrite the file with the modified string.

That depends how big the file is. If it’s a gig or two, you might not
be ABLE to do it that way. If it’s smaller but still fairly large,
you might be able, but it would cause you to start swapping. Besides,
that’s not all THAT much easier.

Or you can use -i on the Ruby command line for in-place regex subbing,
just like in Perl.

-Dave