Shell

將 shell 結果轉換為列中的 Excel 文件

  • August 10, 2020

我正在執行一個給出以下結果的 shell 腳本,我想將結果儲存在 Excel 中的不同列(HOST、Status、Expires、Days)中。如何轉換成 Excel?

Host                                            Status       Expires      Days
----------------------------------------------- ------------ ------------ ----
FILE:certs/dnscert.crt                          Valid        Aug  4, 2021  359

給定您的範例輸入的修改:

$ cat input
Host                                            Status       Expires      Days
----------------------------------------------- ------------ ------------ ----
FILE:certs/dnscert1.crt                         Valid        Aug  1, 2020  7
FILE:certs/dnscert2.crt                         Invalid      Aug  4, 2021  359
FILE:certs/dnscert3.crt                         Valid        Aug  4, 2021  359

然後,如果我應用以下awk命令,我會得到:

$ awk 'NR > 2 { print $1 "," $2 ",\"" $3, $4, $5 "\"," $6}' input
FILE:certs/dnscert1.crt,Valid,"Aug 1, 2020",7
FILE:certs/dnscert2.crt,Invalid,"Aug 4, 2021",359
FILE:certs/dnscert3.crt,Valid,"Aug 4, 2021",359

假設日期欄位始終是 3 個標記:月、日和年。

NR > 2跳過前兩行:標題和分隔符。其餘的只是列印帶有逗號的欄位,注意引用日期欄位。

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