Linux

如何組合兩個 awk 命令

  • March 5, 2020

在我的屬性文件中,我確實有這樣的數據

domainurl=https://example.com
abc.host=10.5.39.23#10.15.1.5
locator.port=1013#1013
db.cassandra.contactPoints=10.15.13.28,10.35.73.16,10.35.93.4

我確實有以下兩個命令,它們單獨工作正常,但是當我嘗試組合這些命令時,我沒有得到預期的輸出。這是命令

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)'$file | grep '^[^#]'
| awk '/http:\/\//  {print $2,80} 
      /https:\/\// {print $2,443} 
      /Points/     {print $2,"NA"} 
      /host/       {h=$2} 
      /port/       {print h,$2; h=""}'

這是 excel 表中逗號分隔值的另一個命令

awk -F"," '{for(i=1;i<=NF;i++){if(i%NF==0){ORS="\n"} {if($i ~ /^\"/ || $i ~ /\"$/) {a=a OFS $i;j++;{if(j%2==0){sub(/^[[:space:]]/,X,a); print a;j=0;a=""}}} else {print $i}}}}'

當我結合這是最終腳本時,它沒有按預期工作

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)' abc.properties | grep '^[^#]' | awk '{split($0,a,"="); print a[1],a[2]}' | awk -F"," '{for(i=1;i<=NF;i++){if(i%NF==0){ORS="\n"} {if($i ~ /^\"/ || $i ~ /\"$/) {a=a OFS $i;j++;{if(j%2==0){sub(/^[[:space:]]/,X,a); print a;j=0;a=""}}} else {print $i}}}}' | awk '/http:\/\//  {print $2,80} 
      /https:\/\// {print $2,443} 
      /Points/     {print $2,"NA"} 
      /host/       {h=$2} 
      /port/       {print h,$2; h=""}' | column -t

實際輸出

http://example.com                                                                   80
10.15.13.28                                                                         NA
10.5.39.23#10.15.1.5                                                              1013#1013

預期產出

http://example.com                                                                     80
10.15.13.28                                                                           NA
10.35.73.16                                                                           NA
 10.35.93.4                                                                            NA
10.5.39.23                                                                           1013
10.15.1.5                                                                            1013

注意:逗號(,)分隔和雜湊(#)分隔需要進入csv文件的下一列

預先感謝您的回答。

$  grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b|https.*" /tmp/test.txt
https://example.com
10.5.39.23
10.15.1.5
10.15.13.28
10.35.73.16
10.35.93.4

$ cat /tmp/test.txt
domainurl=https://example.com
abc.host=10.5.39.23#10.15.1.5
locator.port=1013#1013
db.cassandra.contactPoints=10.15.13.28,10.35.73.16,10.35.93.4

感謝您的建議和答案,我已經想出瞭如何使用’|’ 符號,這是得到預期結果的最終程式碼。

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)' $file | grep '^[^#]'| awk '{split($0,a,"="); print a[1],a[2]}'|sed 's/^\|#/,/g'| awk '/http:\/\//  {print $2,80} 
      /https:\/\// {print $2,443} 
      /Points/     {print $2,"9042"} 
      /host/       {h=$2} 
      /port/       {print h,$2; h=""}'|awk -F'[, ]' '{for(i=1;i<NF;i++){print $i,$NF}}'|column -t

引用自:https://unix.stackexchange.com/questions/571226