excel - 如何使用shell脚本将结果保存在excel中的单独列中

标签 excel bash shell awk

我将执行的输出保存在 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.

关于excel - 如何使用shell脚本将结果保存在excel中的单独列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60518164/

相关文章:

bash shell : lost first element data partially

Linux脚本按行中的日期时间模式过滤文本文件中的行

vba - Excel VBA - Vlookup

excel - 取消 GetSaveAsFilename

bash - 如何在 bash 中的 heredoc 之后使用控制运算符

linux - 我的 bash 脚本 (linux) 中的错误计数器在 while 语句期间不计数

bash - 我可以只 grep 文件的前 n 行吗?

linux - Bash 脚本如果为 null,则为 true 时评估为 false

excel - 我可以用 Excel 中的 VBA 保存功能替换 "Save"或 "Save as"按钮吗?

Excel MATCH 范围,没有特定的 CELL