problems iterating in RUBY while extracting info from YAML, Pls help!


 
Thread Tools Search this Thread
Top Forums Programming problems iterating in RUBY while extracting info from YAML, Pls help!
# 1  
Old 10-04-2008
problems iterating in RUBY while extracting info from YAML, Pls help!

Hi all,
I am stuck with a ruby script that extracts detials from yaml file and processes accordingly.
the yaml file
Code:
confivnic:
  device:
    vnic1:
      policy:
      - L2
      mode: active
    vnic2:
      policy:
      - L3
      - L4
      mode: active
  type: aggr

notsoconfi:
  device:
    vnic9:
      policy:
      - L2
      - L3
      mode: "off"
    vnic3:
      policy:
      - L2
      - L3
      mode: active
    vnic6:
      policy:
      - L3
      - L2
      mode: active
  type: aggr

the ruby script
Code:
require 'yaml'
yaml = YAML::load(File.open("testyaml.yaml"))

yaml.each do |name, option|
if option['type'] == "aggr"
names = Array.new
names.push name
while names.size != 0
key = yaml.keys.first
tpl = "dladm create-{{{type}}} {{{sets}}} {{{cmd}}}"
tpl.gsub! '{{{type}}}', yaml[key]['type']
tpl.gsub! '{{{cmd}}}',  key
sets = ''
set_tpl = '-l {{{key}}} -L={{{mode}}} -P={{{policy}}} '
yaml[key]['device'].each do |device|
  set = set_tpl.dup
  set.gsub! '{{{key}}}',    device[0]
  set.gsub! '{{{mode}}}',   device[1]['mode']
  set.gsub! '{{{policy}}}', device[1]['policy'].join(',')
  sets << set
end
names.pop
end
tpl.gsub! '{{{sets}}}', sets
puts tpl.gsub(/\s+/,' ')
end
end

The o/p i get is like this
Code:
dladm create-aggr -l vnic9 -L=off -P=L2,L3 -l vnic3 -L=active -P=L2,L3 -l vnic6 -L=active -P=L3,L2 notsoconfi
dladm create-aggr -l vnic9 -L=off -P=L2,L3 -l vnic3 -L=active -P=L2,L3 -l vnic6 -L=active -P=L3,L2 notsoconfi

but the yaml file has 2 names ,"confivnic" and "notsoconfi"
This was written originally for just one module but for obvious reasons we had to change it. I tried all i knew but really not finding a way out.
The things that i feel are creating errors are yaml.keys.first
or the way iteration is done.

The o/p i want is similar to the shown one but instead of repeating the same one twice i want the other one to be displayed as well with the necessary changes.

Pls help
# 2  
Old 10-05-2008
Yes, one of your problems is the repeated calls to yaml.keys.first. (will always return 'notsoconfi')

The following code delivers the expected output:
Code:
require 'yaml'

set_tpl = '-l {device} -L={mode} -P={policy} '

yaml = YAML::load(File.open("testyaml.yaml"))

yaml.each do |name, option|
   if option['type'] == "aggr"
      tpl = 'dladm create-{type} {sets} {cmd}'
      tpl.gsub! '{type}', option['type']
      tpl.gsub! '{cmd}', name

      sets = ""
      yaml[name]['device'].each do |device|
         set = set_tpl.dup
         set.gsub! '{device}', device[0]
         set.gsub! '{mode}',   device[1]['mode']
         set.gsub! '{policy}', device[1]['policy'].join(',')
         sets << set
      end

      tpl.gsub! '{sets}', sets
      puts tpl.gsub(/\s+/,' ')
   end
end

Code:
dladm create-aggr -l vnic9 -L=off -P=L2,L3 -l vnic3 -L=active -P=L2,L3 -l vnic6 -L=active -P=L3,L2 notsoconfi
dladm create-aggr -l vnic1 -L=active -P=L2 -l vnic2 -L=active -P=L3,L4 confivnic

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Have a situation while extracting info

i have a text file which i am generating from few sqls. format is like : col1 col2 col3 col4 col5 1001 DONE ABC 17-sep-14 12:02:05 1001 DONE ABC 17-sep-14 12:02:05 1001 DONE ABC 17-sep-14 12:02:55 1001 REDONE ABC ... (6 Replies)
Discussion started by: deepakiniimt
6 Replies

2. UNIX for Dummies Questions & Answers

Extracting specific info finger command

how to extract user machine name for current terminal using finger command below command gives machinename for all session , is it possible to filter it to only currernt terminal ? finger -b -p $LOGNAME | grep from (12 Replies)
Discussion started by: lalitpct
12 Replies

3. Solaris

Extracting HBA Card Hardware info

Hello, I am very new to solaris so please bear with me. I have 2 machines in question. For both I am trying to get the HBA Card Hardware information such as: HBA Model Name HBA Firmware version HBA Port details HBA Driver details First machine is a Solaris 10. When I execute fcinfo... (6 Replies)
Discussion started by: flagman5
6 Replies

4. Web Development

Permission problems calling Ruby from php script

I'm just getting my feet wet with web development, so hopefully this is a simple thing I'm overlooking, but so far I'm stumped. I have a php script that calls Ruby via exec(). This works fine in my test environment, but when I moved it to my production environment I run into a permissions... (7 Replies)
Discussion started by: dantes990
7 Replies

5. Shell Programming and Scripting

matching and extracting info from text files

Hi all, I have two .txt file i.e. First text file: 2 4 1 4 Second text file 2 1.nii.gz 4 334.nii.gz 1 12.nii.gz 4 134.nii.gz If entry in 1st column of 1st text file matches the 1st column of 2nd text file, then copy the file (name of which is the second column) associated with... (4 Replies)
Discussion started by: vd24
4 Replies

6. Shell Programming and Scripting

Parsing file, yaml file? Extracting specific sections

Here is a data file, which I believe is in YAML. I am trying to retrieve just the 'addon_domains" section, which doesnt seem to be as easy as I had originally thought. Any help on this would be greatly appreciated!! I have been trying to do this in awk and mostly bash scripting instead of perl... (3 Replies)
Discussion started by: Rhije
3 Replies

7. UNIX for Dummies Questions & Answers

Extracting spec info from finger.

Hi everyone, I'm trying to extract the user name and full name from the finger command without using sed or awk. Any pointers? Thanks in advance. (6 Replies)
Discussion started by: franny
6 Replies

8. UNIX for Dummies Questions & Answers

Extracting specific info from finger command

Hello all, my unix is bash based and the finger command output is: Login Name Tty Idle LoginTime Office amos.john Amos John pts/26 1 Dec 5 16:18 (77.100.22.07) What am trying to achieve is extract the Login (amos.john) and Name (Amos John) from this output without using awk or sed. ... (1 Reply)
Discussion started by: franny
1 Replies

9. UNIX for Dummies Questions & Answers

Extracting Info

i have a file that contain lines like this 9.4.7.8.5.7.9.0.5.7.1.2.msisdn.sub.cs. 1 IN CNAME SDP01.cs. there are about 50,000 lines like this in the files i want to the extract the digits from the above line like:- 947857905712 OS Solaris9 (3 Replies)
Discussion started by: muneebr
3 Replies

10. UNIX for Advanced & Expert Users

extracting info from Unix database to construct a visual diagram

Ok heres the situation, We use Solaris 8 at work with Sybase for the db. I need to be able to easily create visual diagrams of some of our more complex systems. I've been using Visio which is such a manual process and takes a while. I was thinking maybe using Visio somehow in conjunction... (0 Replies)
Discussion started by: fusion99
0 Replies
Login or Register to Ask a Question