Ruby for Sysadmin

R

Ron Mr.

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!
 
J

James Edward Gray II

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!

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 Gray II
 
L

Logan Capaldo

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!
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
 
J

John Gabriele

I need to write a script that pulls usernames our of a file that is
formatted like the following.

[snip]

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]
[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.
 
L

Logan Capaldo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top