Text-Processing

從日誌文件中的多行轉換紀元時間

  • October 19, 2017

我正在嘗試找到一種從日誌文件的每一行批量轉換紀元時間戳的方法。

據我了解,我應該遵循這個過程。

  1. 在日誌中搜尋紀元時間戳的所有迭代
  2. 將它們通過一個將它們轉換為人類可讀格式的函式
  3. 將它們編輯回正常的日誌文件輸出

不幸的是,我似乎無法正確獲取格式,這是日誌文件的一小段:

"[1508371955] Auto-save of retention data completed successfully.",
"[1508375555] Auto-save of retention data completed successfully.",
"[1508379155] Auto-save of retention data completed successfully.",
"[1508382755] Auto-save of retention data completed successfully.",
"[1508386355] Auto-save of retention data completed successfully."
awk -F"[][]" '{cmd="date -d@" $2;cmd |getline $2; close(cmd)}1' infile

" Thu Oct 19 03:42:35 STD 2017  Auto-save of retention data completed successfully.",
" Thu Oct 19 04:42:35 STD 2017  Auto-save of retention data completed successfully.",
" Thu Oct 19 05:42:35 STD 2017  Auto-save of retention data completed successfully.",
" Thu Oct 19 06:42:35 STD 2017  Auto-save of retention data completed successfully.",
" Thu Oct 19 07:42:35 STD 2017  Auto-save of retention data completed successfully."

在這裡,我們將一個 shell 命令設置date -d@$2為一個名為cmd. 然後cmd |getline $2讀取awk一行並將第二個欄位值傳遞給cmd我們打開它,然後我們需要關閉我們通過管道打開的命令,所以我們close(cmd)在這裡使用。

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