首页 > 解决方案 > 如何通过读取非标准属性文件生成csv文件

问题描述

我有没有标准格式的示例文件,需要生成具有以下条件的 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

条件:

  1. 仅考虑 URL 和 Host name ,应考虑 IP 地址,例如带有 .com 或 .org 的端点或 IP。
  2. 删除 http:// 和 Https://
  3. 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

标签: bashshellif-statementawksed

解决方案


这可能对您有用(GNU grep 和 sed):

grep -Eio '([0-9]{1,3}\.){3}[0-9]{1,3}(:[0-9]{1,4})?|https?://[^/]*\.(com|org)(:[0-9]{1,4})?' file |
sed -E '1{x;s/^/hostname port/p;x};s#^https?://##I;/:/!s/$/:9042/;y/:/ /'

解决方案分为两部分:

  • 使用 grep 提取 IP 和/或 URL
  • 使用 sed 添加标头和添加/拆分端口

推荐阅读