Ruby for Sysadmin

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 D. Adler 00-9388 x0753 Engineering”

I also need to have it use a default password or “[email protected]!”

Any tips or source code examples would be great, thanks!

On Oct 2, 2006, at 10:17 AM, Ron Mr. wrote:

the group for example…

cdaeng

The GECOS field would be “Charles D. Adler 00-9388 x0753
Engineering”

I also need to have it use a default password or “[email protected]!”

Any tips or source code examples would be great, thanks!

Try reading the lines one at a time. See if you can locate the name
and group. Then try munging those into what you need.

If you get stuck post your code and a description of the problem and
we’ll get you going again.

Good luck!

James Edward G. II

On Tue, Oct 03, 2006 at 12:17:24AM +0900, Ron Mr. wrote:

the group for example…
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 D. 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

On 10/2/06, Logan C. [email protected] wrote:

DATA.each do |line|
if md = line.match(/^"([^"]+)"\s*(.*)$/)
name, remaining_fields = md[1], md[2]
[snip]

Just curious, why do you backslash escape the double-quote marks? I
don’t think they’re special inside a regex, and the match works
without the backslashes.

On Tue, Oct 03, 2006 at 03:02:04AM +0900, John G. wrote:

Just curious, why do you backslash escape the double-quote marks? I
don’t think they’re special inside a regex, and the match works
without the backslashes.
No good reason, I was fiddling with this while I wrote it, and forgot to
take those out

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs