Loading ...
Sorry, an error occurred while loading the content.

[PBML] Re: 2 Questions ...

Expand Messages
  • Dan Boger
    On Wed, 02 Feb 2000 11:35:46 -0200 Suporte Técnico NetAlfa wrote ... well, since you can t tell perl to change just the one line, you
    Message 1 of 7 , Feb 2, 2000
    • 0 Attachment
      On Wed, 02 Feb 2000 11:35:46 -0200 Suporte Técnico NetAlfa <suporte@...> wrote
      concerning '[PBML] 2 Questions ...':
      > Hello all,
      >
      > It's my first message to this list, and I have 2 questions:
      >
      > 1) I need to change a line in a file. I know how to add a
      > simple line, but I need to rewrite a line. For example:
      > # cat file
      > line 1
      > line 2
      > line 3
      > I wanna rewrite the second line to "changed".

      well, since you can't tell perl to change just the one line, you have
      to scan the whole file:

      open (FILE, "file.txt") or die "Can't open file: $!";
      open (NEW, ">new.txt") or die "Can't write new: $!";
      while (<FILE>) {
      if ($_ eq 'line 2') { # you can use ~= to match just a part of the line
      s/line 2/changed/; # do whatever changes you want in the line
      print NEW $_; # print it to the new file
      print NEW <FILE>; # print all the rest of the old file to the new one
      last; # all done
      } else {
      print NEW $_;
      }
      }
      close (FILE) or warn "Couldn't close file.txt: $!";
      close (NEW) or warn "Couldn't close new.txt: $!";
      rename ("file.txt","file.txt.old");
      rename ("new.txt","file.txt");

      this of course does not take care of locking, or any kind of race
      conditions - but it's one way of doing it.


      >
      > 2) Anybody here knows how to get the number of days
      > since 1970? I can't do it with the linux date function!
      >

      $days = time()/60/60/24;

      that should do the trick - time() returns the number of seconds since
      epoch - 1/1/1970 on unix;

      Dan
    • Dan Boger
      ... just curious - why the seperation of the $header_line from the rest of the @lines? Dan
      Message 2 of 7 , Feb 2, 2000
      • 0 Attachment
        > open (MYFILE, "<myfile.txt")
        > or die "Can't open myfile.txt, $!";
        > ($header_line, @lines) = <MYFILE>;
        > close (MYFILE);
        > chomp ($header_line, @lines);

        just curious - why the seperation of the $header_line from the rest of
        the @lines?

        Dan
      • Jeff Boes
        ... Any online bookseller is likely to carry it: www.bookpool.com www.fatbrain.com www.amazon.com You might even find a copy at www.ebay.com. I don t know
        Message 3 of 7 , Feb 2, 2000
        • 0 Attachment
          > -----Original Message-----
          > From: root@... [mailto:root@...]On
          > Behalf Of Suporte Técnico NetAlfa
          > Sent: Wednesday, February 02, 2000 9:24 AM
          > To: perl-beginner@egroups.com
          > Subject: [PBML] Re: 2 Questions ...
          >
          >
          > Jeff,
          >
          > >
          > > A very good source for answers to questions like these is the
          > Perl Cookbook.
          > > I highly recommend it as your second Perl book.
          >
          > Thank you so much!
          > Where cai I find the Perl Cookbook? One of the reasons
          > that I'm in this list is that I don't find a good manual...
          >

          Any online bookseller is likely to carry it:

          www.bookpool.com
          www.fatbrain.com
          www.amazon.com

          You might even find a copy at www.ebay.com. I don't know which source would
          be best for international shipping, however.

          ----
          "To paraphrase Mark Twain, the difference between the right program and
          almost the right program is like the difference between lightning and a
          lightning bug. The difference is just a bug." --Danny Hillis, "The Pattern
          on the Stone" (1998)
          ___________
          Jeff Boes <>< jboes@...
          Mur Consulting http://www.qtm.net/~jboes/
        • Jeff Boes
          ... No particular reason--the original file looked like the first line was special in some way, so I wanted to illustrate how you could separate it upon
          Message 4 of 7 , Feb 2, 2000
          • 0 Attachment
            > -----Original Message-----
            > From: Dan Boger [mailto:dan@...]
            > Sent: Wednesday, February 02, 2000 11:18 AM
            > To: perl-beginner@egroups.com; jboes@...
            > Subject: [PBML] Re: 2 Questions ...
            >
            >
            > > open (MYFILE, "<myfile.txt")
            > > or die "Can't open myfile.txt, $!";
            > > ($header_line, @lines) = <MYFILE>;
            > > close (MYFILE);
            > > chomp ($header_line, @lines);
            >
            > just curious - why the seperation of the $header_line from the rest of
            > the @lines?
            >

            No particular reason--the original file looked like the first line was
            "special" in some way, so I wanted to illustrate how you could separate it
            upon loading. That's clearer than slurping the whole file into one array and
            then dancing around the first line, if you need to make some kind of global
            change.

            ----
            "The future masters of technology will have to be lighthearted and
            intelligent. The machine easily masters the grim and the dumb."
            --Marshall McLuhan
            ___________________
            Jeff Boes <>< jboes@...
            Mur Consulting http://www.qtm.net/~jboes/
          Your message has been successfully submitted and would be delivered to recipients shortly.