Converting shell/awk to ruby


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Converting shell/awk to ruby

any idea on how to get started with this:

shell script:

Code:
awk '/{/,/}/' ~/newservices.txt | while read line
do
        BEGIN=$(echo "${line}" | egrep ":" | egrep "{")
        if [ ! -z "${BEGIN}" ] ; then
                checkname=$(echo $line | awk -F":" '{print $1}' | sed 's_"__g')
                echo "{" > /tmp/services/monitoring_${checkname}_vip.json
                echo '    "id": "'"monitoring_${checkname}_vip"'",' >> /tmp/services/monitoring_${checkname}_vip.json
                continue
        fi
        USENRPE=$(echo "${line}" | egrep "nrpe_args")
        if [ ! -z "${USENRPE}" ] ; then
                echo "    ${line}" >> /tmp/services/monitoring_${checkname}_vip.json
                echo '    "use_existing_command": "nrpe_args",' >> /tmp/services/monitoring_${checkname}_vip.json
                continue
        fi
        END=$(echo "${line}" | egrep "}")
        if [ ! -z "${END}" ] && [ -z "${USENRPE}" ] ; then
                echo "${line}" | sed 's_,__g' >> /tmp/services/monitoring_${checkname}_vip.json
                continue
        else
                echo "    ${line}" | sed 's~service_description~description~g' >> /tmp/services/monitoring_${checkname}_vip.json
        fi
done

I want to convert the above to ruby but here's how far i've gotten so far yes, i know its pretty bad Smilie

Code:
#!/usr/bin/env ruby

file_names = [ 'newservices.txt' ]
file_names.each do |file_name|
  text = File.read(file_name)
  new_contents = text.gsub(/command/, "mike")

  # To merely print the contents of the file, use:
  puts new_contents

  # To write changes to the file, use:
  #File.open(file_name, "w") {|file| file.puts new_contents }
end

the contents of newservices.txt looks something like this:
Code:
                                "monitoring_mf_hpiu_unload_disk":{
                                        "service_description": "MainFrame Storage - /hpiu_unloads/apptables Disk",
                                        "contact_groups": [ "HQ-CLT-INFY-DG" ],
                                        "_TKTQC": "Corp L2 Infra Unix queue",
                                        "command": "nrpe_args",
                                        "command_options": [ "check_disks", "'-w 21% -c 20% -p /hpiu_unloads/apptables'" ],
                                        "notifications_enabled": 0
                                }

# 2  
It would have been much better if you would had posted what you really wanted as output, instead of some shell code that it is full of problems.

The following is a solution heavily based on a guess:

Code:
#!/usr/bin/env ruby

block = []
path = "/tmp/services/"
filename = ""

File.readlines("newservices.txt").each do |line|
    if line =~ /"(.+)":\s*{/
        filename = "#{path}monitoring_#{$1}_vip.json"
        block.push "{", "\t\"id\": \"monitoring_#{$1}_vip\","
        next
    end
    if block.any?
        if line.include? "}"
           block << line.strip.chomp
           File.open(filename, "w"){|f| f.puts block}
           block.clear
           filename.clear
           next
        end
        block << line.chomp.gsub(/^\s+/, "\t")
        if line.include? "nrpe_args"
            block << "\t\"use_existing_command\": \"nrpe_args\","
        end
    end
end


Last edited by Aia; 12-06-2015 at 09:48 PM..
This User Gave Thanks to Aia For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #807
Difficulty: Medium
A key advantage of a MOSFET is that it requires zero input current to control the load current.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting awk to perl

Hello. I'm currently teaching myself Perl and was trying to turn an awk code that I had written into Perl. I have gotten stuck on a particular part and a2p has not helped me at all. The task was to take a .csv file containing a name, assignment type, score and points possible and compute it into a... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. UNIX for Beginners Questions & Answers

Converting awk to perl

Hello. I'm trying to convert an awk script I wrote to perl (which I just started self-teaching). I tried the a2p command but I couldn't make sense of most of it. Here was the awk code: BEGIN{ FS = "," print "NAME\tLOW\tHIGH\tAVERAGE" a=0 } { if(a==0){ a+=1 (1 Reply)
Discussion started by: Eric7giants
1 Replies

3. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

4. Shell Programming and Scripting

Using awk for converting xml to txt

Hi, I have a xml script, I converted it to .txt with values comma seperated using awk function. But I want the output values should be inside double quotes My xml script (Workorders.xml) is shown like below: <?xml version="1.0" encoding="utf-8" ?> <scbm-extract version="3.3">... (8 Replies)
Discussion started by: Viswanatheee55
8 Replies

5. Shell Programming and Scripting

Converting to matrix-like file using AWK

Hi, Needs for statistics, doing converting Here is a sample file Input : 1|A|17,94 1|B|22,59 1|C|56,93 2|A|63,71 2|C|23,92 5|B|19,49 5|C|67,58 expecting something like that Output : 1|A|17,94|B|22,59|C|56,93 2|A|63,71|B|0|C|23,92 5|A|0|B|19,49|C|67,58 (11 Replies)
Discussion started by: fastlane3000
11 Replies

6. Shell Programming and Scripting

Converting grep to awk

First, I am trying to search for certain string types within a very large file. Here is a sample record: MyCountry|MyGroup|MyCust|Run-Date|N/A|SERVER|OS|USERID|897//123456//LNAME FNAME|STATE|LLOGON In field 9 I am looking for invalid formats. A valid format would be 897/C/123456//LNAME FNAME.... (25 Replies)
Discussion started by: dagamier
25 Replies

7. UNIX for Dummies Questions & Answers

Converting HP-UX awk to Solaris

Hi, I am using awk in HP-UX to enter an encrypted entry of the password into /etc/passwd with success, this is the command I am using and it is working great. cat /tmp/passwd.gal.before|awk -F: -v gal_passwd="encrypted_password" '{OFS=":" ; print $1,gal_passwd,$3,$4,$5,$6,$7}' >... (3 Replies)
Discussion started by: galuzan
3 Replies

8. Shell Programming and Scripting

Converting Perl code to shell

I have a perl code that runs like Code I sub p { if ($d >= $D) {return} printf "%3s"x$d++," "; printf "%s%s\n",$_,$h{$_}?" ** ":""; if (!$h{$_}) { $h{$_}=1; map {p($_)} @{$s{$_}} } $d-- } ($Set,$Job,$Num,$D) = (@ARGV); map {shift} 0..3; (8 Replies)
Discussion started by: zainravi
8 Replies

9. Shell Programming and Scripting

How to get shell commands working through ruby?

Hi all, I am a newbie currently trying to execute shell commands from ruby instead of a shell script.(This method is conceived only for solaris so no issues with porting to other OS and all) Eg: Consider the command 'ls" with a shell script ,I would use it like this bash# ls <all the... (4 Replies)
Discussion started by: wrapster
4 Replies

10. UNIX for Dummies Questions & Answers

Converting bash shell to perl

Does anyone know how to convert this bash shell script to perl? for i in `ls -l *pgp` do `usr/bin/gpg --passphrase-fd 0 $i < .sunspot` done Thanks! Any help would be appreciated. I'm new to Linux and perl. (4 Replies)
Discussion started by: freak
4 Replies

Featured Tech Videos