2014-09-05 123 views
2

我想将这个Shell脚本转换成Ruby脚本。 我已经完成了Rubular Regex,但是我不知道如何将reg ex隐含到ruby脚本中。Shell脚本>红宝石

Shell脚本:

#!/bin/bash 
file="./NA_Hosts.txt" 
      if [ ! -e $file ];then 
          sed -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/.ipss.test.net. 60 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
          -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/.ipss.test.net. 60 IN A /,/g' /tmp/testing/dig_whole_ipss.txt > ./NA_Hosts.txt 
       echo ${txtgrn}Script executed successfully.${txtrst} 
       cat ./NA_Hosts.txt >> ./NA_Temp.txt 
          exit 
         else 
       read -r -p "${txtred}ATTENTION!${txtrst} Generating this file will replace the old one. Do you wish to continue? [y/N] " response 
       if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]];then 
        sed -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/.ipss.test.net. 60 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
           -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/.ipss.test.net. 60 IN A /,/g' /tmp/testing/dig_whole_ipss.txt > ./NA_Hosts.txt 
        changecount=`cat NA_Temp.txt NA_Hosts.txt | sort | uniq -u | wc -l` 
           echo "There have been made ${txtred}$changecount${txtrst} total changes." 
           echo ${txtgrn}NA-Hosts file has been successfully updated! ${txtrst} 
        cat NA_Hosts.txt|grep ""s-vd"" >> dslamlist.txt 
        cat NA_Hosts.txt|grep ""s-vh"" >> dslamlist.txt 
        cat ./NA_Hosts.txt > ./NA_Temp.txt 
        if [[ "$changecount">"$sendmailtreshold" ]];then 
         mail -s "Notification: NA-Host File updated!" [email protected]<<-MYTEXT 
         Node DB | NA-Host File 
         The NA-Host File has been successfully updated. 
         There has been made $changecount changes. 
         You're receiving this notification because you are a on the list of receivers. 
         *** This is an automatically generated email, please do not reply *** 
         MYTEXT 
        fi 
       else 
           echo ${txtred}Script aborted.${txtrst} 
          exit 
        fi 

shell脚本工作正常。

Rubular REG EX:

(ipc-\D{3,4}\d{3}-s-v.-\d{2})\.ipss.test.net\.\s+\d+\s+IN\s+A\s+(\d+\.\d+\.\d+\.\d+) 

的Ruby脚本的目标应该是让这种格式:

ipc-zuc650-s-vd-04.ipss.test.net. 3600 IN A 10.150.90.110 

进入这个:

ipc-zuc650-s-vd-04,10.150.90.110 
+0

找到我读的标题*外壳脚本优于红宝石*,这是一个有争议点。 .. – zetetic 2014-09-05 21:25:48

回答

1

这里的其中一个办法你在做什么:

regex = /(ipc-\D{3,4}\d{3}-s-v.-\d{2})\.ipss.test.net\.\s+\d+\s+IN\s+A\s+(\d+\.\d+\.\d+\.\d+)/ 
string = "ipc-zuc650-s-vd-04.ipss.test.net. 3600 IN A 10.150.90.110" 
string.scan(regex).map{ |host,ip| "#{host},#{ip}" } 

这导致阵列如下:["ipc-zuc650-s-vd-04,10.150.90.110"]弦乐#扫描

文档可以在http://www.ruby-doc.org/core-2.1.2/String.html#method-i-scan