Perl equivalent to unix script

M

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.

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

Thanks

Mike
 
M

Mike

Mike said:
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
 
J

jl_post

Mike said:
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 said:
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
 
B

Ben Morrow

[newsgroups trimmed, f'ups set]

Quoth "Mike said:
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
 
U

usenet

Mike said:
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__
 
T

Tintin

Mike said:
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?
 
M

Mumia W. (reading news)

It looks like a one-liner in Perl.

perldoc File::Slurp
perldoc -f sort
 
D

Dr.Ruud

Dan Mercer schreef:
usenet:

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

Dr.Ruud

Dan Mercer schreef:
Dr.Ruud:

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

Peter J. Holzer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top