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

[PBML] 2 Questions ...

Expand Messages
  • Suporte Técnico NetAlfa
    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
    Message 1 of 7 , Feb 2, 2000
    • 0 Attachment
      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".

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

      Please, if anybody here knows the answer of any
      questions here, please contact me.


      --
      Thanks in advance,

      André Volpato
      NetAlfa Support
      www.netalfa.com.br
      suporte@...
      UIN / ICQ:13180370
    • Jeff Boes
      ... A very good source for answers to questions like these is the Perl Cookbook. I highly recommend it as your second Perl book. ... Perl s sequential file
      Message 2 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 8:36 AM
        > To: perl-beginner@egroups.com
        > Subject: [PBML] 2 Questions ...
        >
        >
        > Hello all,
        >
        > It's my first message to this list, and I have 2 questions:

        A very good source for answers to questions like these is the Perl Cookbook.
        I highly recommend it as your second Perl book.

        >
        > 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".

        Perl's sequential file access (and it's not just Perl, but most operating
        systems that limit this) won't let you do a straight rewrite of a line. You
        can rewrite (overwrite) a sequence of _bytes_, but if the new line is not
        exactly the same length as the old, the result will be junk.

        Here's how I'd do it (and as always, with Perl TMTOWTDI [There's More Than
        One Way To Do It!, http://www.netmeg.net/jargon/terms/t/TMTOWTDI.html%5d):

        open (MYFILE, "<myfile.txt")
        or die "Can't open myfile.txt, $!";
        ($header_line, @lines) = <MYFILE>;
        close (MYFILE);
        chomp ($header_line, @lines);

        # Manipulate the lines, for example:
        $lines[1] = 'changed';

        open (MYFILE, ">myfile.txt")
        or die "Can't open myfile.txt, $!";
        print join("\n", $header_line, @lines), "\n";
        close (MYFILE);

        The disadvantage of this approach: the file can't be "locked" between the
        reads and the writes, so in a shared environment (such as a CGI script) this
        is vulnerable to a race condition. In that case, you want to open the file
        for update:

        open (MYFILE, "+>myfile.txt")...

        Then lock the file:

        flock (MYFILE)
        or die "Can't lock MYFILE";

        Then instead of closing and reopening, you rewind:

        seek(MYFILE,0,0)
        or die "Can't position MYFILE";

        and before you really close it, you truncate it:

        truncate(MYFILE, tell MYFILE)
        or die "Can't truncate MYFILE";

        (This last step takes care of trailing junk if you just made the file
        shorter.)


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

        Easy. The time() function returns 'Epoch' seconds--number of seconds since
        the start of the Epoch, which is 1/1/1970. (Beware! On a Mac, the Epoch
        started on 1/1/1904. No, I don't know why.)

        $epoch_secs = time();
        $epoch_days = $epoch_secs / (60 * 60 * 24);

        ----
        "Last among the essential personality traits for programming, we might list
        _sense of humor_. The computer 'doth make fools of us all,' so that any
        fool without the ability to share a laugh on himself will be unable to
        tolerate programming for long. It has been said with great perspicacity
        that the Programmer's National Anthem is 'aaaaahhhhh.' When we finally see
        the light, we see how once again we have fallen into some foolish
        assumption, some oafish practice, or some witless blunder. Only by singing
        the second stanza, 'ha ha ha ha ha,' can we long endure the role of the
        clown."
        --Gerald Weinberg, _The Psychology of Computer Programming_
      • Suporte Técnico NetAlfa
        Jeff, ... 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... -- Thanks in
        Message 3 of 7 , Feb 2, 2000
        • 0 Attachment
          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...

          --
          Thanks in advance,

          André Volpato
          NetAlfa Support
          www.netalfa.com.br
          suporte@...
          UIN / ICQ:13180370
        • 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 4 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 5 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 6 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 7 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.