Ask a Question related to PERL Beginners, Design and Development.
-
Sharad Gupta #1
Packaging my stuff
Hi All,
I am sure people must be doing that regularly.
How do i package my Application to give to the customer??.
Like creating packages for different platforms linux, debian, sun, windows etc.
Any pointers would be greatly appreciated.
Thanx,
-Sharad
Sharad Gupta Guest
-
Packaging and Deployment
So I've banged my head against a wall for a couple of hours this morning trying to get the deployment manager to work... I don't want to have to... -
Packaging the project
How can i package my project to a WAR file K,Browne Developer -
Packaging looking for fonts not used (on XP)
I'm just starting with Indesign CS (long time PageMaker user) on my Windows XP box. When running "Save for Service Provider", the preflight (and... -
Packaging applications
Reading the red book 'Developing and Porting C and C++ Applications on AIX', it seems that mkinstallp is only provided from AIX 5.2. Is there any... -
packaging .dll's
Good afternoon I used 8.0 and just upgraded to MX. I am still confused about putting the .dll's in the xtras folder. We have done about 8... -
Dean Do #2
Sort Text File
Hello,
I'm rather new to PERL. How do I go about sorting
a simple text file using PERL? This is what I want
to accomplish.
# sort /etc/passwd > /etc/passwd-new
# mv /etc/passwd-new /etc/passwd
Thanks in advance for your help.
-Dean
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com[/url]).
Version: 6.0.517 / Virus Database: 315 - Release Date: 9/8/03
Dean Do Guest
-
James Edward Gray II #3
Re: Sort Text File
On Friday, October 3, 2003, at 01:45 PM, Dean Do wrote:
Howdy.> Hello,
Any good reason not to use the above? :)> I'm rather new to PERL. How do I go about sorting
> a simple text file using PERL? This is what I want
> to accomplish.
>
> # sort /etc/passwd > /etc/passwd-new
> # mv /etc/passwd-new /etc/passwd
Well, what would be the steps?
1. open /etc/passwd/
2. read all entries into memory
3. close /etc/passwd/
4. sort entries
5. open new file
6. print all entries
7. close new file
8. rename new file
Can you walk down that list replacing it with Perl statements?
James
James Edward Gray II Guest
-
John W. Krahn #4
Re: Sort Text File
Dean Do wrote:
Hello,>
> Hello,
Are you sure you want to modify /etc/passwd? Which field do you want to sort on?> I'm rather new to PERL. How do I go about sorting
> a simple text file using PERL? This is what I want
> to accomplish.
>
> # sort /etc/passwd > /etc/passwd-new
> # mv /etc/passwd-new /etc/passwd
#!/usr/bin/perl
# *** UNTESTED ***
use warnings;
use strict;
use Fcntl qw( :flock :seek );
my $file = '/etc/passwd';
open my $fh, '+<', $file or die "Cannot open $file: $!";
flock $fh, LOCK_EX or die "Cannot lock $file: $!";
# sort on the user name field
my @records = map $_->[ 1 ],
sort { $a->[ 0 ] cmp $b->[ 0 ] }
map [ (split /:/)[ 0 ], $_ ],
<$fh>;
# sort on the UID field
#my @records = map $_->[ 1 ],
# sort { $a->[ 0 ] <=> $b->[ 0 ] }
# map [ (split /:/)[ 2 ], $_ ],
# <$fh>;
seek $fh, 0, SEEK_SET or die "Cannot seek on $file: $!";
# you could also use truncate() here but since you are not
# changing the size of the data it is not necessary.
print $fh @records or die "Cannot print to $file: $!";
# close and unlock the file
close $fh;
__END__
If you are going to try this out, MAKE A BACKUP COPY of the file first!
If you screw up the /etc/passwd file without a backup you are going to
be in a world of hurt.
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Randal L. Schwartz #5
Re: Sort Text File
>>>>> "John" == John W Krahn <krahnj@acm.org> writes:
John> Are you sure you want to modify /etc/passwd? Which field do you
John> want to sort on?
And if you want to modify /etc/passwd, you should be using vipw,
because vipw will lock the file for you, and verify some sanity checks
the updated data to keep you from shooting yourself in the foot.
#!/usr/bin/perl
unless (@ARGV) {
# no argument passed, so call ourselves inside vipw:
$ENV{EDITOR} = $0; # vipw will call us now instead of vi/emacs/whatever
exec "vipw";
die "Cannot exec vipw: $!";
}
# argument passed means that vipw has called us with a temp filename
# edit it inplace
$^I = "~";
# and all at once (for reasons that are hard to explain here)
$/ = undef;
my @lines = split /\n/, <>; # only one read gets the entire file
# anything we print replaces that entire file
@lines = sort @lines; # here's the active step - replace as you wish
print map "$_\n", @lines;
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Randal L. Schwartz Guest



Reply With Quote

