add string into the file (somewhere in the middle)

add string into the file (somewhere in the middle)

am 21.04.2008 21:11:23 von Jahoo

Hello
I'm wondering howto make such thing:
i would like to add specific string into some configuration files.

but not in the end of file (it's obviously easy) but somewhere in the
middle
for example
in my.cnf after line [mysqld] I'd like to add:
key_buffer = 16M

or in /etc/xinetd.d/echo after lines:
service echo
{
disable = yes

I'd like to add:
wait = yes

of course after my string there has to be rest of file...

any ideas?

regards
jahooo

Re: add string into the file (somewhere in the middle)

am 21.04.2008 22:03:40 von luiheidsgoeroe

On Mon, 21 Apr 2008 21:11:23 +0200, Jahoo wrote:

> Hello
> I'm wondering howto make such thing:
> i would like to add specific string into some configuration files.
>
> but not in the end of file (it's obviously easy) but somewhere in the
> middle
> for example
> in my.cnf after line [mysqld] I'd like to add:
> key_buffer =3D 16M
>
> or in /etc/xinetd.d/echo after lines:
> service echo
> {
> disable =3D yes
>
> I'd like to add:
> wait =3D yes
>
> of course after my string there has to be rest of file...

There's no way to 'insert' it at a specific point, save for loading the =
=

file in memory, making the alteration in the string, and writing it back=
..
-- =

Rik Wasmus

Re: add string into the file (somewhere in the middle)

am 21.04.2008 23:12:21 von Charles Calvert

On Mon, 21 Apr 2008 19:11:23 +0000 (UTC), Jahoo wrote in
:

>I'm wondering howto make such thing:
>i would like to add specific string into some configuration files.
>
>but not in the end of file (it's obviously easy) but somewhere in the
>middle
>for example
>in my.cnf after line [mysqld] I'd like to add:
>key_buffer = 16M
>
>or in /etc/xinetd.d/echo after lines:
>service echo
>{
> disable = yes
>
>I'd like to add:
> wait = yes

What you're looking for is an algorithm. It would be something like
this:

open file
get first line
while you have a line
check the line to see if its the one after
which you want to insert lines
if so, insert the lines
get next line
end while
close file

You should start reading the reference on file functions
.
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research

Re: add string into the file (somewhere in the middle)

am 22.04.2008 03:23:45 von freelance71

"Jahoo" wrote in message news:fuiosq$vk8$6@news.onet.pl...
> Hello
> I'm wondering howto make such thing:
> i would like to add specific string into some configuration files.
>
> but not in the end of file (it's obviously easy) but somewhere in the
> middle
> for example
> in my.cnf after line [mysqld] I'd like to add:
> key_buffer = 16M
>
> or in /etc/xinetd.d/echo after lines:
> service echo
> {
> disable = yes
>
> I'd like to add:
> wait = yes
>
> of course after my string there has to be rest of file...
>
> any ideas?
>
> regards
> jahooo


Read the file line by line and write each line to a new file. When you get
to the line after which you want to insert extra lines just write those
lines to the new file then continue with the rest. At the end delete the old
file and rename the new file to old file if you have to.

Re: add string into the file (somewhere in the middle)

am 22.04.2008 16:03:56 von Charles Calvert

On Mon, 21 Apr 2008 17:12:21 -0400, Charles Calvert
wrote in :

[snip]

Correction:

>What you're looking for is an algorithm. It would be something like
>this:
>
>open file
>get first line
>while you have a line

write the line to the output file

> check the line to see if its the one after
> which you want to insert lines
> if so, insert the lines
> get next line
>end while
>close file
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research