What I’m trying to do is format matching text from a file. Currently
I’ve
got the basic output working, but ideally I’d like to do two additional
things.
- Sort the paragraphs of output by group name, alphabetically.
- Remove from output, Editors who are also a Primary or Secondary.
For #2 I tried doing the following, which didn’t work. For some reason
/primary/ is not seen as a variable:
puts "Primary: " + primary = $1 if key =~ /owner/ &&
value.match(/uid=(.),ou/)
puts "Editor: " + $1 if key =~ /uniquemember/ &&
value.match(/uid=(.),ou/)
&& !~ /primary/
Below is where I’m currently at with this. Any help would be
appreciated.
Thanks.
[Current code]:
File.readlines(‘test_groups.txt’).each do |line|
unless line.strip.empty?
myhash = Hash[line.strip.split(":")]
myhash.each do |key, value|
puts "Group: " + $1 if key =~ /dn/ && value.match(/cn=(.),ou/)
puts "Primary: " + $1 if key =~ /owner/ &&
value.match(/uid=(.),ou/)
puts "Secondary: " + $1 if key =~ /seeAlso/ &&
value.match(/uid=(.),ou/)
puts "Editor: " + $1 if key =~ /uniquemember/ &&
value.match(/uid=(.*),ou/)
end
end
puts “\n” if line.strip.empty?
end
[Current output]:
Group: group2
Primary: user1
Secondary: user2
Editor: user1
Editor: user2
Group: group1
Primary: user8
Secondary: user6
Editor: user8
Editor: user11
Editor: user20
Editor: user6
[Preferred output]:
Group: group1
Primary: user8
Secondary: user6
Editor: user11
Editor: user20
Group: group2
Primary: user1
Secondary: user2
[File contents of: test_groups.txt]:
dn: cn=group2,ou=groups,dc=example,dc=com
owner: uid=user1,ou=people,dc=example,dc=com
seeAlso: uid=user2,ou=people,dc=example,dc=com
uniquemember: uid=user1,ou=people,dc=example,dc=com
uniquemember: uid=user2,ou=people,dc=example,dc=com
dn: cn=group1,ou=groups,dc=example,dc=com
owner: uid=user8,ou=people,dc=example,dc=com
seeAlso: uid=user6,ou=people,dc=example,dc=com
uniquemember: uid=user8,ou=people,dc=example,dc=com
uniquemember: uid=user11,ou=people,dc=example,dc=com
uniquemember: uid=user20,ou=people,dc=example,dc=com