find a "expression" listed at last in a script

Discussion in 'Programming/Scripts' started by rumak18, Dec 9, 2010.

  1. rumak18

    rumak18 New Member


    i'm trying to write a script, that goes through a file, that has entries like:


    Now i want to add an entry like "4_ftp" , but only after the "3_ftp".
    That's why ""echo "4_ftp" >> file" won't work.
    So how could i solve this issue?
  2. rodobrist

    rodobrist New Member

    you have probably already thought of this, but dismissed it due to its lack of 'elegence'
    but if I needed to get the job done, I would:

    1. write all of those values into an array using explode(). using carriage return delimiter,
    2. either with a while/for or foreach loop keep writing the lines into a new file until you get where you want to be(continually checking with an if statement).
    3. then write in a new line, and continue along the previous loop.

    So by the end of it you should have a file that is almost identical to your original file.
    Then you can over write the old file with the new one.

    As i said, not elegant, but it should work with relatively known commands.

Share This Page