Perl equivalent to unix script

Discussion in 'Perl Misc' started by Mike, Oct 30, 2006.

  1. Mike

    Mike Guest

    Ok. . . Well, I'm sure here comes another dumb question. I'm fairly
    handy with unix and unix scripting, however, I'm terrible at perl.

    What I'm looking to do with a perl script is the equivalent of the
    following unix (bash) script:

    cat tempfile1 | sort > newfile2; rm tempfile1

    I'm not completely comfortable with file handling in perl, and I think
    it should be easy to do, but I've been unsuccessful in trying to do it.

    I'm essentially trying to sort the lines in a file (alphabetically),
    and then output them to another file.

    Thanks

    Mike
     
    Mike, Oct 30, 2006
    #1
    1. Advertising

  2. Mike

    Mike Guest

    Mike wrote:
    > Ok. . . Well, I'm sure here comes another dumb question. I'm fairly
    > handy with unix and unix scripting, however, I'm terrible at perl.
    >
    > What I'm looking to do with a perl script is the equivalent of the
    > following unix (bash) script:
    >
    > cat tempfile1 | sort > newfile2; rm tempfile1
    >
    > I'm not completely comfortable with file handling in perl, and I think
    > it should be easy to do, but I've been unsuccessful in trying to do it.
    >
    > I'm essentially trying to sort the lines in a file (alphabetically),
    > and then output them to another file.
    >
    > Thanks
    >
    > Mike


    I guess I should have said that I don't want to handle this with a
    system call either, I'd prefer to figure out, and learn how to do it
    with purely perl.

    Thanks

    Mike
     
    Mike, Oct 30, 2006
    #2
    1. Advertising

  3. Mike

    Guest

    Mike wrote:
    > What I'm looking to do with a perl script is the equivalent of the
    > following unix (bash) script:
    >
    > cat tempfile1 | sort > newfile2; rm tempfile1


    Just use the command:

    perl -e 'print sort <>' tempfile1 > newfile2; rm tempfile1

    > I'm fairly handy with unix and unix
    > scripting, however, I'm terrible at perl.


    Ah... yeah... I was the same way a few years ago: I was (fairly)
    good at Unix scripting, but didn't know any Perl. All that changed
    when I read O'Reilly's "Learning Perl" book (by Randal L. Schwartz and
    Tom Phoenix). The chapters are easy to understand and the exercises
    are laid out very well. The book will teach you things all decent Perl
    programmers should know, as well as things that will save you literally
    hours of debugging time later on. (It will also explain '<>' (the
    diamond operator) in sufficient detail so that you can use it to
    program quickly and more efficiently.)

    I hope this helps, Mike.

    -- Jean-Luc
     
    , Oct 30, 2006
    #3
  4. Mike

    J. Gleixner Guest

    Mike wrote:
    > Mike wrote:
    >> Ok. . . Well, I'm sure here comes another dumb question. I'm fairly
    >> handy with unix and unix scripting, however, I'm terrible at perl.
    >>
    >> What I'm looking to do with a perl script is the equivalent of the
    >> following unix (bash) script:
    >>
    >> cat tempfile1 | sort > newfile2; rm tempfile1
    >>
    >> I'm not completely comfortable with file handling in perl, and I think
    >> it should be easy to do, but I've been unsuccessful in trying to do it.


    Post what you've tried.

    >>
    >> I'm essentially trying to sort the lines in a file (alphabetically),
    >> and then output them to another file.


    >
    > I guess I should have said that I don't want to handle this with a
    > system call either, I'd prefer to figure out, and learn how to do it
    > with purely perl.


    If you want to figure it out, give the documentation a try.

    perldoc -f open
    perldoc -f sort
    perldoc -f unlink
    perldoc opentut

    Helpful Web sites:

    http://perldoc.perl.org/
    http://bookmarks.cpan.org/search.cgi?cat=Training/Tutorials
     
    J. Gleixner, Oct 30, 2006
    #4
  5. Mike

    Ben Morrow Guest

    [newsgroups trimmed, f'ups set]

    Quoth "Mike" <>:
    > Ok. . . Well, I'm sure here comes another dumb question. I'm fairly
    > handy with unix and unix scripting, however, I'm terrible at perl.
    >
    > What I'm looking to do with a perl script is the equivalent of the
    > following unix (bash) script:
    >
    > cat tempfile1 | sort > newfile2; rm tempfile1
    >
    > I'm not completely comfortable with file handling in perl, and I think
    > it should be easy to do, but I've been unsuccessful in trying to do it.


    perldoc -f open
    "I/O Operators" in perlop
    perldoc -f sort
    perldoc -f print
    perldoc -f unlink

    Make an attempt to solve the problem yourself. If you fail, post what
    you have, and we will help you fix it.

    Ben

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
     
    Ben Morrow, Oct 30, 2006
    #5
  6. Mike

    Guest

    Mike wrote:

    > cat tempfile1 | sort > newfile2;


    That's rather convoluted even for UNIX scripting. Why not:

    sort tempfile1 > newfile2;

    You cannot do this as a pure-Perl approach (ie, no shell redirects,
    etc) any more simply. You could do something like this:

    #!/usr/bin/perl
    use strict; use warnings;

    open (my $in, '<', '/tmp/tempfile1') or die "oops - $!\n";
    open (my $out, '>', '/tmp/newfile2') or die "oops - $!\n";

    print $out sort(<$in>);

    close $in;
    close $out;

    unlink $in;

    __END__

    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
     
    , Oct 30, 2006
    #6
  7. Mike

    Tintin Guest

    "Mike" <> wrote in message
    news:...
    > Ok. . . Well, I'm sure here comes another dumb question. I'm fairly
    > handy with unix and unix scripting, however, I'm terrible at perl.
    >
    > What I'm looking to do with a perl script is the equivalent of the
    > following unix (bash) script:
    >
    > cat tempfile1 | sort > newfile2; rm tempfile1


    Handy at Unix, but still using UUOC?




    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Tintin, Oct 30, 2006
    #7
  8. > Quoth "Mike" <>:
    >> Ok. . . Well, I'm sure here comes another dumb question. I'm fairly
    >> handy with unix and unix scripting, however, I'm terrible at perl.
    >>
    >> What I'm looking to do with a perl script is the equivalent of the
    >> following unix (bash) script:
    >>
    >> cat tempfile1 | sort > newfile2; rm tempfile1
    >>
    >> I'm not completely comfortable with file handling in perl, and I think
    >> it should be easy to do, but I've been unsuccessful in trying to do it.


    It looks like a one-liner in Perl.

    perldoc File::Slurp
    perldoc -f sort


    --
     
    Mumia W. (reading news), Nov 1, 2006
    #8
  9. On 2006-11-01 04:45, Ignoramus18920 <ignoramus18920@NOSPAM.18920.invalid> wrote:
    > On Wed, 01 Nov 2006 03:58:33 GMT, Mumia W. (reading news) <> wrote:
    >>> Quoth "Mike" <>:
    >>>> cat tempfile1 | sort > newfile2; rm tempfile1

    >>
    >> It looks like a one-liner in Perl.
    >>
    >> perldoc File::Slurp
    >> perldoc -f sort
    >>
    >>

    >
    > Now try sorting a 4 GB file with that... Something that GNU sort can
    > easily do...


    Use the right tool for the job.

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Nov 1, 2006
    #9
  10. Mike

    Dr.Ruud Guest

    Dan Mercer schreef:
    > usenet:
    >> Mike:


    >>> cat tempfile1 | sort > newfile2;

    >>
    >> That's rather convoluted even for UNIX scripting. Why not:
    >> sort tempfile1 > newfile2;

    >
    > Even that's too convoluted - why not just
    > sort -o file file


    Because that is not equivalent. For example you can't tell from what is
    presented whether tempfile1 is still needed for something else, after
    the sort.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Nov 2, 2006
    #10
  11. Mike

    Dr.Ruud Guest

    Dan Mercer schreef:
    > Dr.Ruud:
    >> Dan Mercer:
    >>> usenet:
    >>>> Mike:


    >>>>> cat tempfile1 | sort > newfile2;
    >>>>
    >>>> That's rather convoluted even for UNIX scripting. Why not:
    >>>> sort tempfile1 > newfile2;
    >>>
    >>> Even that's too convoluted - why not just
    >>> sort -o file file

    >>
    >> Because that is not equivalent. For example you can't tell from what
    >> is presented whether tempfile1 is still needed for something else,
    >> after the sort.

    >
    > The original pipeline was:
    >
    > cat tempfile1 | sort > newfile2; rm tempfile1


    Fine, but you replied on a message with a changed one.

    If sort would somehow fail, I assume (some copy of) "file" will remain
    available, after a "sort -o file file".

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Nov 3, 2006
    #11
  12. On 2006-11-03 12:55, Dr.Ruud <> wrote:
    > If sort would somehow fail, I assume (some copy of) "file" will remain
    > available, after a "sort -o file file".


    Depends on the implementation. With GNU sort this is not guaranteed.
    After file has been completely read and (partially) sorted, it is opened
    for writing. If GNU sort fails after that you are left with only a
    partial copy. If you have enough space it is safer to invoke

    sort file > file.$$ && mv file.$$ file

    hp

    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Nov 3, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,806
  2. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    281
    Michele Dondi
    Jan 22, 2004
  3. Replies:
    4
    Views:
    412
    John W. Krahn
    Jan 31, 2005
  4. Replies:
    6
    Views:
    107
    Tad McClellan
    Jul 17, 2006
  5. Melroy

    Perl equivalent of unix time command

    Melroy, Sep 7, 2008, in forum: Perl Misc
    Replies:
    13
    Views:
    209
Loading...

Share This Page