On Tue, Oct 03, 2006 at 12:17:24AM +0900, Ron Mr. wrote:
> I need to write a script that pulls usernames our of a file that is 
> formatted like the following.
> 
> "ADLER, CHARLES DAVID"  00-9388 x0753   Engineering
> "ANSELL, ROBERT D"      14-2675 x1624   Sales
> "BIANCHI, CHRISTOPHER"  12-2275 x3280   Marketing
> "CAPOZZI, MICHAEL B"    08-3191 x8035   Manufacturing
> 
> The last column is what group the user is in.
> The username needs to be the initials plus the first three letters of 
> the group for example...
> 
> cdaeng
> 
> The GECOS field would be "Charles David Adler 00-9388 x0753 Engineering"
> 
> I also need to have it use a default password or "p@ssw0rd!"
> 
> Any tips or source code examples would be great, thanks!
> 
> -- 
> Posted via http://www.ruby-forum.com/.
Hope this helps:

% cat parse.rb
#!/usr/bin/env ruby

DATA.each do |line|
  if md = line.match(/^\"([^"]+)\"\s*(.*)$/)
    name, remaining_fields = md[1], md[2]
    number, extension, group = remaining_fields.split
    last_name, first_name, middle_name = name.split(/,?\s+/)
    puts "Username: #{first_name[0,1].downcase}#{middle_name.to_s[0,1].downcase}#{last_name[0,1].downcase}#{group[0, 3].downcase}"
   nice_name = [first_name, middle_name, last_name].map { |s| 
      s.to_s.downcase.capitalize }.reject { |s| s == "" }.join(' ')
   puts "GECOS: #{nice_name} #{number} #{extension} #{group}"
 else
  unless line =~ /^\s*$/ # if  the line isn't blank, we print an error
   STDERR.puts "Warning, mal-formed line: #{line}"
  end
 end
end

__END__
"ADLER, CHARLES DAVID"  00-9388 x0753   Engineering                             
"ANSELL, ROBERT D"      14-2675 x1624   Sales                                   
"BIANCHI, CHRISTOPHER"  12-2275 x3280   Marketing                               
"CAPOZZI, MICHAEL B"    08-3191 x8035   Manufacturing                           

% ruby parse.rb
Username: cdaeng
GECOS: Charles David Adler 00-9388 x0753 Engineering
Username: rdasal
GECOS: Robert D Ansell 14-2675 x1624 Sales
Username: cbmar
GECOS: Christopher Bianchi 12-2275 x3280 Marketing
Username: mbcman
GECOS: Michael B Capozzi 08-3191 x8035 Manufacturing