Operating System - HP-UX
1827295 Members
4003 Online
109717 Solutions
New Discussion

perl split function question

 
SOLVED
Go to solution
Jeff Picton
Regular Advisor

perl split function question

Hi

I have a mixed delimiter password file with both : and ,,,.

How can I use the perl split function to load the password file into an array without the delimeters ?

I know this is easy with awk but what about split ?

Jeff
9 REPLIES 9
Brian Bergstrand
Honored Contributor

Re: perl split function question

@pwent = split(/:/, $_);
@gecos = split(/,/, $pwent[4]);

HTH.
Paddy_1
Valued Contributor

Re: perl split function question

Here is a example that can serve to help :
word can be split into characters, a sentence split into words and a paragraph split into sentences:

@chars = split(//, $word);
@words = split(/ /, $sentence);
@sentences = split(/\./, $paragraph);

The sufficiency of my merit is to know that my merit is NOT sufficient
Jeff Picton
Regular Advisor

Re: perl split function question

Hi

Thats fine but I need to print the WHOLE file (as an array) without delimeters. Brians suggestion only enables one to print a single field of the array.
Brian Bergstrand
Honored Contributor

Re: perl split function question

I'm not sure what you mean. You want each line of the file as a separate entry in an array? And each entry should be void of the : and , delimeters?

Could you better describe your requirements?

Thanks.
Jordan Bean
Honored Contributor
Solution

Re: perl split function question

You actually have two distinct options: split and getpwent. The latter works on the system passwd file only.

If you want to sort, then I'd use a hash. Otherwise, I'd print each line as its read and munged. A simple array isn't necessary.

I'm pulling the following snippets from memory, so please excuse any errors.

1. No sorting:

perl -pe 'tr/:,/ /;' < passwd > passwd.out

2. Sorting:

$h={};
while(<>){ chomp; ($u,@u)=split /[:,]/; $h->{$u}=[@u]; }
foreach ( sort keys %$h ) { printf '%s 'x9."\n", $_, @{$h->{$_}}; }

3. System routine. No sorting:

setpwent;
while(@_=getpwent){printf '%s 'x9."\n",@_;}
endpwent;

4. System routine. Sorted:

$h={};
setpwent;
while(($u,@u)=getpwent){ $h->{$u}=[@u]; }
endpwent;
foreach ( sort keys %$h ) { printf '%s 'x9."\n", $_, @{$h->{$_}}; }

It's late. Let me know if I goofed.
Mark Grant
Honored Contributor

Re: perl split function question

If I understand the question correctly you want to finish up with one array that contains every line of the password file but without delimiters. Simplest thing here is to just translate all the delimiters to spaces as in "tr /:/ /;" and "tr /,/ /;" and "push" the result onto an array.

If you want to access each individual field of each individual line of the file you will need a multi-dimentional array. Multi-dimentional arrays are a pain in perl, in my view so I would go with the previous suggest and make a hash of arrays. First translate the ","'s to spaces (or to ":" if you want them as seperate fields) then split on the ":" into an array which you then put into a hash as in "$myhash{$array[0]}=[ @array ].

You would access an user fred'd uid for example with $myhash{fred}[2].
Never preceed any demonstration with anything more predictive than "watch this"
H.Merijn Brand (procura
Honored Contributor

Re: perl split function question

Why not use perl's buildin functions to access the password file info?

perldoc -f endpwent
perldoc -f getpwent
perldoc -f getpwnam
perldoc -f getpwuid
perldoc -f setpwent

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Jeff Picton
Regular Advisor

Re: perl split function question

Hi

Thanks for all the replies.

I tried the sort method but there is a syntax error on the printf statement which I cannot find

Jeff
Jordan Bean
Honored Contributor

Re: perl split function question

Hmm. Interesting. Put a space between '%s 'x9 and ."\n".