gpt4 book ai didi

ruby - shell 脚本 > ruby

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:27 25 4
gpt4 key购买 nike

我正在尝试将此 Shell 脚本转换为 Ruby 脚本。我已经完成了 Rubular Regex,但我不知道如何将 reg ex 暗示到 ruby​​ 脚本中。

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!" John.test@test.com<<-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 正则表达式:

(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

最佳答案

这里有一种方法可以完成您想要做的事情:

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"]

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

关于ruby - shell 脚本 > ruby ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com