我将执行的输出保存在excel工作表中,结果将显示在新行中,如下所示

我确实有这样的要求,

db.cassandra.contactPoints     10.11.12.13
db.cassandra.port                8080
server.host                10.10.10.10:9443
server2.host             10.12.12.12:9001
sftp.host             abcdiefgh.unix.hes2000.org
sftp.port                     22


Expected output

filename                   hostname          Port
db.cassandra.contactPoints  10.11.12.13        8080
server.host                 10.10.10.10        9443
server2.host                10.12.12.12        9001
sftp.host          abcdiefgh.unix.hes2000.org   22

我用下面的脚本

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)' $filename | awk 'BEGIN {print "Column_A\tColumn_B"}NR%2 {if(next = "%port%"){printf "%s \t",$0;next;}}1' | grep '^[^#]' | awk '{split($0,a,"="); print a[1],","a[2]}'

预先感谢您的建议

分析解答

您可以尝试以下吗?

awk '
BEGIN{
  print "Filename hostname port"
}
/:[0-9]+/{
  sub(/:/,"\t")
  print;
  next
}
NF==2{
  if($NF~/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ || $NF~/[a-zA-Z]+\..*\.[a-zA-Z]+$/){
    val=$0
  }
  else{
    if(val){
      print val,$NF
      val=""
    }
  }
}
' Input_file | column -t

说明:在此处为上述代码添加详细说明。

awk '                                                                             ##Starting awk program from here.
BEGIN{                                                                            ##Starting BEGIN section of this program here.
  print "Filename hostname port"                                                  ##Printing header for output here.
}
/:[0-9]+/{                                                                        ##Checking condition if line has colon digits then do following.
  sub(/:/,"\t")                                                                   ##Substitute colon with TAB here in line.
  print;                                                                          ##Printing the current line here.
  next                                                                            ##next will skip all further statements from here.
}
NF==2{                                                                            ##Checking condition if number of fields in a Line are 2 then do following.
  if($NF~/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ || $NF~/[a-zA-Z]+\..*\.[a-zA-Z]+$/){    ##If a line is in ip format or domain format then do following.
    val=$0                                                                        ##Creating variable val which has current line value in it.
  }
  else{                                                                           ##Mentioning else here.
    if(val){                                                                      ##Checking condition if variable val is NOT NULL then do following.
      print val,$NF                                                               ##Printing variable val and last field of line here.
      val=""                                                                      ##Nullifying variable val here.
    }
  }
}
'  Input_file | column -t                                                              ##Mentioning Input_file name here and passing whole output to column command.