add string into the file (somewhere in the middle)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jahoo

    #1

    add string into the file (somewhere in the middle)

    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
  • Rik Wasmus

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

    On Mon, 21 Apr 2008 21:11:23 +0200, Jahoo <jahoo@o2.plwro te:
    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...
    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

    Comment

    • Charles Calvert

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

      On Mon, 21 Apr 2008 19:11:23 +0000 (UTC), Jahoo <jahoo@o2.plwro te in
      <fuiosq$vk8$6@n ews.onet.pl>:
      >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
      <http://www.php.net/manual/en/refs.fileproces s.file.php>.
      --
      Charles Calvert | Software Design/Development
      Celtic Wolf, Inc. | Project Management
      http://www.celticwolf.com/ | Technical Writing
      (703) 580-0210 | Research

      Comment

      • freelance71

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


        "Jahoo" <jahoo@o2.plwro te 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.


        Comment

        • Charles Calvert

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

          On Mon, 21 Apr 2008 17:12:21 -0400, Charles Calvert <cbciv@yahoo.co m>
          wrote in <o40q04pn8jh1r7 cv2pct4ul66flpn 39kug@4ax.com>:

          [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

          Comment

          Working...