- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl split function question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 03:16 AM
10-16-2003 03:16 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 04:04 AM
10-16-2003 04:04 AM
Re: perl split function question
@gecos = split(/,/, $pwent[4]);
HTH.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 04:08 AM
10-16-2003 04:08 AM
Re: perl split function question
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 04:14 AM
10-16-2003 04:14 AM
Re: perl split function question
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 04:25 AM
10-16-2003 04:25 AM
Re: perl split function question
Could you better describe your requirements?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 07:00 PM
10-16-2003 07:00 PM
SolutionIf 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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 07:41 PM
10-16-2003 07:41 PM
Re: perl split function question
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].
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 08:33 PM
10-16-2003 08:33 PM
Re: perl split function question
perldoc -f endpwent
perldoc -f getpwent
perldoc -f getpwnam
perldoc -f getpwuid
perldoc -f setpwent
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 08:41 PM
10-16-2003 08:41 PM
Re: perl split function question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2003 11:41 PM
10-17-2003 11:41 PM