LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2018, 10:18 AM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Rep: Reputation: 18
Question ruby parse init file


Hello,

I'm writing some custom facts for a puppet server.

I need a ruby function for reading a ini file:

eg:

users_prop.cfg

Code:
[user1]
command1 = cmdA
command2 = cmdB
command3 = cmdC

[user2]
command1 = NA

Before someone recommend me : rubygem inifile, puppetmaster + clinets , none of them have internet access, + I've been prohibited to install any 3rd party applications/module from internet w/o special permission, so, I just have to reinvent the wheel...

Any help is appreciated...
 
Old 10-09-2018, 06:01 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So where are you stuck and what have you written so far?
 
Old 10-09-2018, 06:37 AM   #3
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
So where are you stuck and what have you written so far?
Code:
require 'fileutils'
require 'pp'
s = Hash.new{|h,k| h[k] = ''}

file_contents = File.read('/root/xox')
                        File.readlines('/root/xox').each do |line|
                                line = line.chomp
                                 if (line =~ /^\[(.*)\]\s*$/)
                                        section = $1
                                         next
                                end
                                 if (line =~ /^([^=]+?)\s*=\s*(.*?)\s*$/)
                                        param = $1
                                        val   = $2
                                        s[param]  << val
                                 end
                        end

pp s

but I should get a hash like[ "user1"=> command1 => cmdA ,command2 => cmdB, command3 => cmdC, "user2" =>....]

kind of hash of hashes.

I'm not such a pro in ruby.
 
Old 10-09-2018, 10:04 AM   #4
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
So where are you stuck and what have you written so far?
~~~~~~Solved ~~~~~~~~

Code:
require 'fileutils'
require 'pp'
s = Hash.new{|h,k| h[k] = ''}
@data = {}
section = nil

file_contents = File.read('/root/xox')
                        File.readlines('/root/xox').each do |line|
                                line = line.chomp
                                 if (line =~ /^\[(.*)\]\s*$/)
                                        section = $1
                                        @data[section] = {}
                                         next
                                end
                                 if (line =~ /^([^=]+?)\s*=\s*(.*?)\s*$/)
                                        param, val = line.split(/\s*=\s*/, 2)
                                        var_name = "#{param}".chomp.strip
                                        val      = val.chomp.strip
                                        #s[param]  << val
                                        @data[section][var_name] = val
                                 end
                        end


#pp @data


@data.each do |k,q|
        puts "~~~[ #{k} ]~~~~"
        q.each do |a,b|
                  puts a + "===>"+ b
        end
end
 
Old 10-10-2018, 09:00 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I am guessing you have lots of left over cruft you haven't trimmed out of this code and perhaps some other code that may need
some of your extra variables or libraries, but this can be trimmed a littler more to be:
Code:
data = {}
section = nil 

File.readlines('/root/xox').each do |line|
  if /^\[(?<user>[^\]]*)/ =~ line
    section = user
    data[section] = {}
  end 
  if /^(?<lhs>[^=]+)\s*=\s*(?<rhs>.*)\s*$/ =~ line
    data[section][lhs] = rhs 
  end 
end

data.each do |k,q|
  puts "~~~[ #{k} ]~~~~"
  q.each do |a,b|
    puts a + "===>"+ b
  end 
end
Seems to produce the same output and you are welcome to take or leave any of the above
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse a file fanatic_ravi Linux - General 2 06-12-2013 04:52 AM
Parse a CSV file output to text file beto Linux - Newbie 3 04-25-2012 08:45 AM
[SOLVED] Best way to Parse a file Stuart07 Programming 6 05-05-2011 07:56 AM
Ubutnu won't boot. Error: Target file system doesn't have /sbin/init. No init found. Zeljka_Lin Linux - Newbie 9 05-02-2011 06:56 AM
Parse file from remote server to caculate count of string existence in that file saurabhmehan Linux - Newbie 2 08-30-2010 12:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration