need perl help  | |
March 4th, 2003, 03:19 PM
|
#1 (permalink)
| | Junior Member
Join Date: Mar 2003
Posts: 10
|
Hi, I'm new to this message board, and I suck at perl, so if anybody could help, it would be much appreciated.
OK, here's my problem.
I have a list of data in an input file that looks like this:
iep184:30201:222::/showme4/iep184:/usr/lbin/tcsh
The first portion, the iep184 part, is the user ID. I need to go through the list and get all user IDs that start with 'c' and end with '1'.
Any ideas? I think I have to use a split of some sort, but I'm not really sure how that works. Thanks for any help. |
| |
March 4th, 2003, 04:31 PM
|
#2 (permalink)
| | may contain mild peril
Join Date: Oct 2001 Location: UK
Posts: 3,329
|
Sounds like you have the general idea, you could just split the line around : and then check the first field against a simple regexp to see if it matches you criteria. I can hack together some super ugly code for you when I get home if you want, but be warned that I also suck at Perl
Regards
eddy
__________________
I dreamt that a large eagle circled the room three times and then got into bed with me and took all the blankets.
|
| |
March 4th, 2003, 05:58 PM
|
#3 (permalink)
| | may contain mild peril
Join Date: Oct 2001 Location: UK
Posts: 3,329
|
Ok first quick attempt that seems to basically work, but I could be horribly wrong (so please test on a non important file). Code: #!/usr/bin/perl -w
#### DISCLAIMER ###############
#
# Standard warnings, blah blah
# This code was written by a man with the IQ of a punch drunk monkey.
# I accept no responsibility for loss of data, life or sanity.
#
###############################
use strict;
my $usage = <<EOT;
Usage: $0 [filename]
EOT
#do some rather shody checking on no. of args
if (@ARGV != 1) { die $usage }
my $file = shift;
#open an input file
open(FILE, $file) or die "Can't open input: $!";
#step over each line in the file
while(<FILE>){
#split line up into an array
my @line= split(/:/, $_);
#print first element of array if it matches regexp
if($line[0] =~ /^c(.*)1$/){ print "$line[0]\n" }
}
#Tidy up after reading
close(FILE);
#### The madness ends here #### I will spend a few minutes later cleaning it up a bit and hunting down any major bugs, but you get the general idea.
Regards
eddy
Last edited by SpookyEddy : March 4th, 2003 at 06:03 PM.
|
| |
March 4th, 2003, 06:11 PM
|
#4 (permalink)
| | Junior Member
Join Date: Mar 2003
Posts: 10
|
Damn....you kick ass man...I can't thank you enough. One question, what does EOT do? Thanks again. |
| |
March 4th, 2003, 06:16 PM
|
#5 (permalink)
| | may contain mild peril
Join Date: Oct 2001 Location: UK
Posts: 3,329
|
It just lets me read all the info before the last EOT into the variable, handy for writing stuff like multi-line help info you want printing to STDOUT. This would work just as well..... Code: my $usage = "Usage: $0 [filename]" Anyway, hope it works and you are more than welcome. Any problems just shout and I will see if I can fix it.
Later
eddy |
| |
March 4th, 2003, 06:22 PM
|
#6 (permalink)
| | Junior Member
Join Date: Mar 2003
Posts: 10
|
kick ass and chew bubblegum...thanks again mon frere. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |