【发布时间】:2020-05-12 18:03:20
【问题描述】:
我有没有标准格式的示例文件,需要生成具有以下条件的 CSV 文件,我试图找出过去几天的解决方案
ab.db.contact-points=10.135.64.46,10.135.2.6,10.135.8.4
ab.db.port=9042
ab.token.uri=10.135.83.42,10.135.83.41
ab.db.port=9042#9042
ab.token.uri=10.135.83.42
deeplyiourl=https://deeply-internal-npe.example.com/testing/deep/v1/events/
TEST.URL=http://testing.unix.great20000.org:8115/CYBER/SOURCE
ABCDSURL=https://testing-tposs.data.dx-pk1.cf.example.com:443/saveResume/updateDecision?decisionXML={decisionXML}
simple.abcd02.uri=https://abcd02.pro.api.great.example.com/v1/author/testing?grant_type=abcd_credentials
abcd02.defo2-url=tcp://10.158.150.25:7222,tcp://10.158.150.99:72
findingdata.sentry.url=http://create.test.data.com:8555/FirstData/Payment
tstign.endpoint=http://create.test.ext.example.com:6002/ECL1/GatewayV3Proxy/ChargeSale
basic.endpoint=http://Validating/author/testing/v4/internal/test
TEST.URL=http://tesing.great.com
条件:
- 应仅考虑 URL 和主机名、IP 地址,例如带有 .com 或 .org 的端点或 IP。
- 删除http://和https://
- URL 以 .com 或 .org 结尾
- 删除 .com 或 .org 之后的所有内容
- 如果 URL 中有端口信息,请使用该端口信息
我尝试了下面的脚本,但没有得到预期的输出
grep -P '((?<=[^0-9.]|^)[1-9][0-9]{0,2}(\.([0-9]{0,3})){3}(?=[^0-9.]|$)|(http|ftp|https|ftps|sftp)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/+#-]*[\w@?^=%&/+#-])?|\.port|\.host|contact-points|\.uri|\.endpoint)' FASOfflineReviewAction.properties|grep '^[^#]'|awk '{split($0,a,"#"); print a[1]}'|awk '{split($0,a,"="); print a[1],a[2]}'|sed '/.com\|.org\|10.\|17./!d'|sed 's/^\|#/,/g'|awk '/http:\/\// {split($1,a,":");if (a[3] == "") print 80; else print a[3]}
/https:\/\// {split($1,a,":");if (a[3] == "") print 443; else print a[3]}
/Points/ {print $2,"9042"}
/host/ {h=$2}
/port/ {print h,$2; h=""}'|sed 's/com.*/com/'|sed 's/org.*/org/'|awk -F'[, ]' '{for(i=1;i<NF;i++){print $i,$NF}}'|awk 'BEGIN{OFS=","} {$1=$1} 1'|sed '/^[0-9]*$/d'|awk -F, '$1 != $2' |sed -E 's_^https?://__'
期望的输出
hostname port
10.135.64.46 9042
10.135.2.6 9042
10.135.8.4 9042
10.135.83.42 9042
10.135.83.41 9042
10.135.83.42 9042
deeply-internal-npe.example.com 443
testing.unix.great20000.org 8115
testing-tposs.data.dx-pk1.cf.example.com 443
10.158.150.25 7222
10.158.150.99 72
create.test.data.com 8555
create.test.ext.example.com 6002
tesing.great.com 80
【问题讨论】:
-
#9042是评论吗?第二个10.135.83.42(第 5 行)的端口在哪里? -
这可能有助于作为清理的第一步:
grep -Po '(?<==).*' file | sed -En 's|(.*//[^/]*).*|\1|; s/#.*//; /http/{/\.(org|com)/p;d};p' -
@Cyrus 不,该端口没有被注释,它是端口号的分隔,是的,有 IP 地址但没有端口号,如果有 IP 地址,我们需要将默认端口视为 9042
标签: bash shell if-statement awk sed