Sergej: Text in eine bestimmte Zeile einer Datei hinzufügen

Hallo,

Ich möchte einer geöffneten Datei,
die nach einem PerlScript-Durchlauf(Berechnung) beschrieben wurde,
am ende der Berechnung in die 5 Zeile hinschreiben, wie viele Zeilen in der Datei jetzt sind.

Ich hoffe ich hab mich verständlich ausgedrückt.

Gruß Sergej

  1. Halihallo Sergej ;)

    Ich möchte einer geöffneten Datei,
    die nach einem PerlScript-Durchlauf(Berechnung) beschrieben wurde,
    am ende der Berechnung in die 5 Zeile hinschreiben, wie viele Zeilen in der Datei jetzt sind.

    Tja, das geht nicht, ohne die ganze Datei neu zu schreiben. Du weisst ja nicht, bei
    welchem Byte die 5. Zeile anfängt, oder? - Sprich: Du kannst nur absolut nach
    Bytes positionieren, nicht aber nach Zeilen.

    Ich hoffe ich hab mich verständlich ausgedrückt.

    Naja, geht so :-)

    Viele Grüsse

    Philipp

    1. Hi Phil,

      welchem Byte die 5. Zeile anfängt, oder?

      Wenn ich das Ausgabe File mit UltraEdit nach BitStelle durchforste,
      dann möchte ich den Text an die 444 Bit-Stelle hinzufügen.

      und das geht wohl mit seek, oder?

      Gruß Sergej

      1. Halihallo Sergej

        welchem Byte die 5. Zeile anfängt, oder?

        Wenn ich das Ausgabe File mit UltraEdit nach BitStelle durchforste,

        Was UltraEdit, du möchtest das doch wohl automatisieren, oder? - Sonst kannst du es
        gleich von Hand einfügen. Und ein Ändern auf Bitebene macht bei Textfiles keinen Sinn.

        dann möchte ich den Text an die 444 Bit-Stelle hinzufügen.

        Wie kommst du bloss auf Bit? - Ein Zeichen hat ein Byte und das ist die kleinste Einheit,
        die direkt adressierbar ist.

        und das geht wohl mit seek, oder?

        Ja. Wobei seek auf _Byteebene_ arbeitet. Nicht Bit, nicht Ziele.

        Viele Grüsse

        Philipp

        1. Hi Phil,

          natülich meine ich Byte!

          Wenn ich dies eintippe:

          seek(FHOUT, 444, 0);
          print FHOUT "$k Paare gefunden!\n";

          Schreibt er das Ergebnis trotzdem am Ende des Files. ??

          Gruß Sergej

          1. Hi Phil,

            natülich meine ich Byte!

            Wenn ich dies eintippe:

            seek(FHOUT, 444, 0);
            print FHOUT "$k Paare gefunden!\n";

            Schreibt er das Ergebnis trotzdem am Ende des Files. ??

            Wie hast du die Datei geöffnet?
            Du musst sie zum lesen und schreiben öffnen.

            Struppi.

            1. Hallo,

              Wie hast du die Datei geöffnet?
              Du musst sie zum lesen und schreiben öffnen.

              open(FHOUT,"+> $outFile" );
              my $i=0;
              while(<FHOUT>) {
               $i++;
               if ($i == 8){
                print FHOUT "$k Paare gefunden!\n";
               }
              }

              Das Output File ist nach dieser while Schleife leer!
              Ich glaube ich muß was zu futtern holen

              Gruß Sergej

              1. Hallo,

                Wie hast du die Datei geöffnet?
                Du musst sie zum lesen und schreiben öffnen.

                open(FHOUT,"+> $outFile" );

                Damit öffnest du eine Datei zum lesen und schreiben und leerst sie gleichzeitig.
                open FHOUT,"+<$outFile" || die "Kann Datei: $outFile nicht öffnen: $!";

                Das Output File ist nach dieser while Schleife leer!
                Ich glaube ich muß was zu futtern holen

                Sieht so aus ;-)

                Struppi.

                1. Hallo @ll,

                  Hier ist meine Lösung:
                  .
                  ..
                  ...
                  ...Berechnung/Suche und schreibe Ergebnis in Datei $outFile
                  ...
                  ..
                  .
                  #*************************************************************
                  #*                Einfügen der 8 Zeile:                      *
                  #*************************************************************

                  open(FHOLD, "< $outFile")         or die "can't open $outFile: $!";
                  open(FHNEW, "> $outFile2")        or die "can't open $outFile2: $!";
                  while (<FHOLD>) {
                      if ($. == 8) {    # Fügt in die 8 Zeile den untenstehenden Text
                          print FHNEW "* $k Paare gefunden! *\n";
                      }
                      print FHNEW $_;
                  }

                  close(FHOLD);
                  close(FHNEW);
                  unlink($outFile);    # Löscht die alte Datei
                  rename($outFile2, $outFile)       or die "can't rename $outFile to $outFile2: $!";
                  #*************************************************************
                  #*                     ENDE SCRIPT                           *
                  #*************************************************************

                  Danke an alle,

                  Gruß Sergej

  2. Hi folks

    Hier zwei Möglichkeiten aus dem "Perl Cookbook":

    --------------------------------------------------------------------------------------------------
    7.8. Modifying a File in Place with Temporary File
    Problem

    You need to update a file in place, and you can use a temporary file.
    Solution

    Read from the original file, write changes to a temporary file, and then rename the temporary back to the original:

    open(OLD, "< $old")         or die "can't open $old: $!";
    open(NEW, "> $new")         or die "can't open $new: $!";
    while (<OLD>) {
        # change $_, then...
        print NEW $_            or die "can't write $new: $!";
    }
    close(OLD)                  or die "can't close $old: $!";
    close(NEW)                  or die "can't close $new: $!";
    rename($old, "$old.orig")   or die "can't rename $old to $old.orig: $!";
    rename($new, $old)          or die "can't rename $new to $old: $!";

    This is the best way to update a file "in place."
    Discussion

    This technique uses little memory compared to the approach that doesn't use a temporary file. It has the added advantages of giving you a backup file and being easier and safer to program.

    You can make the same changes to the file using this technique that you can with the version that uses no temporary file. For instance, to insert lines at line 20:

    while (<OLD>) {
        if ($. == 20) {
            print NEW "Extra line 1\n";
            print NEW "Extra line 2\n";
        }
        print NEW $_;
    }

    Or delete lines 20 through 30:

    while (<OLD>) {
        next if 20 .. 30;
        print NEW $_;
    }

    Note that rename won't work across filesystems, so you should create your temporary file in the same directory as the file being modified.

    The truly paranoid programmer would lock the file during the update.

    -------------------------------------------------------------------------------------------------------

    7.10. Modifying a File in Place Without a Temporary File
    Problem

    You need to insert, delete, or change one or more lines in a file, and you don't want to (or can't) use a temporary file.
    Solution

    Open the file in update mode ("+<"), read the whole file into an array of lines, change the array, then rewrite the file and truncate it to its current seek pointer.

    open(FH, "+< FILE")                 or die "Opening: $!";
    @ARRAY = <FH>;

    change ARRAY here

    seek(FH,0,0)                        or die "Seeking: $!";
    print FH @ARRAY                     or die "Printing: $!";
    truncate(FH,tell(FH))               or die "Truncating: $!";
    close(FH)                           or die "Closing: $!";

    Discussion

    As explained in the Introduction, the operating system treats files as unstructured streams of bytes. This makes it impossible to insert, modify, or change bits of the file in place. (Except for the special case of fixed-record-length files, discussed in Recipe 8.13.) You can use a temporary file to hold the changed output, or you can read the entire file into memory, change it, and write it back out again.

    Reading everything into memory works for small files, but it doesn't scale well. Trying it on your 800 MB web server log files will either deplete your virtual memory or thrash your machine's VM system. For small files, though, this works:

    open(F, "+< $infile")       or die "can't read $infile: $!";
    $out = '';
    while (<F>) {
        s/DATE/localtime/eg;
        $out .= $_;
    }
    seek(F, 0, 0)               or die "can't seek to start of $infile: $!";
    print F $out                or die "can't print to $infile: $!";
    truncate(F, tell(F))        or die "can't truncate $infile: $!";
    close(F)                    or die "can't close $infile: $!";

    For other examples of the things you can do in-place, look at the recipes in Chapter 8.

    This approach is for the truly determined. It's harder to write, takes more memory (potentially a lot more), doesn't keep a backup file, and may confuse other processes trying to read from the file you're updating. For most purposes, therefore, we suggest it's probably not worth it.

    Remember to lock if you're paranoid.
    ---------------------------------------------------------------------------------------------------------

    Vielleicht hilft das ja etwas weiter.

    Bye
    Timothy

    --
    Zwei Dinge im Leben kannst du nicht zurück holen. Den Pfeil, den du verschossen. Und das Wort, das du gesprochen.
    (alte indianische Weisheit)
    1. WOW Timothy,

      You are the best!
      That's what i need.

      Thank you.
      Sergej

      1. Hi

        You are the best!

        Nö - die hier sind es

        Tom Christiansen & Nathan Torkington

        That's what i need.

        Das freut mich.

        Wie heißt es so schön: "Wissen heißt: Wissen wo es steht".

        In diesem Sinne

        Bye
        Timothy

        --
        Zwei Dinge im Leben kannst du nicht zurück holen. Den Pfeil, den du verschossen. Und das Wort, das du gesprochen.
        (alte indianische Weisheit)