# Ruby for Sysadmin

**URL:** <https://www.ruby-forum.com/t/ruby-for-sysadmin/70753>\
**Category:** Ruby\
**Created:** [October 2, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/ruby-for-sysadmin/70753 "2006-10-02T17:17:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [October 2, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/ruby-for-sysadmin/70753/1 "2006-10-02T17:17:22Z")

</div>

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 “p@ssw0rd!”

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

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [October 2, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/ruby-for-sysadmin/70753/2 "2006-10-02T17:25:17Z")

</div>

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 “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 G. II

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [October 2, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/ruby-for-sysadmin/70753/3 "2006-10-02T17:51:22Z")

</div>

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

> the group for example…  
> Posted via [http://www.ruby-forum.com/](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

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [October 2, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/ruby-for-sysadmin/70753/4 "2006-10-02T20:03:42Z")

</div>

On 10/2/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [October 2, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/ruby-for-sysadmin/70753/5 "2006-10-02T22:03:56Z")

</div>

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
