Bash
如何從每一行輸出不同的數據?
我有以下文件:
File: ‘./Payment_Volume_and_Value_Report_000000501C5_2.10_2022_11_14_06_24_49.xml’ Modify: 2022-11-14 06:24:54.466847421 -0500 Change: 2022-11-14 06:25:02.166883414 -0500 File: ‘./Payment_Volume_and_Value_Report_000000501C5_2.9_2022_11_14_06_24_54.xml’ Modify: 2022-11-14 06:24:54.740847211 -0500 Change: 2022-11-14 06:25:02.166883414 -0500 File: ‘./Payment_Volume_and_Value_Report_000000501C5_2022_11_14_06_24_54.xml’ Modify: 2022-11-14 06:24:54.637847290 -0500 Change: 2022-11-14 06:25:02.166883414 -0500
我希望每個文件的輸出看起來像這樣:
Payment_Volume_and_Value_Report_000000501C5_2.10_2022_11_14_06_24_49.xml 06:24:54 06:25:02
如果可能的話,最後我想在顯示的兩個小時之間做出區別(更改-修改)
Payment_Volume_and_Value_Report_000000501C5_2.10_2022_11_14_06_24_49.xml 00:00:08
使用bash和 GNU
date
命令:while read -r attr data; do [[ $attr == File: ]] && echo "$data" [[ $attr == Modify: ]] && m="$data" if [[ $attr == Change: ]]; then c="$data" c_epoch=$(date -d"$c" +%s) m_epoch=$(date -d"$m" +%s) echo "$((c_epoch - m_epoch)) seconds" fi done < file
輸出:
‘./Payment_Volume_and_Value_Report_000000501C5_2.10_2022_11_14_06_24_49.xml’ 8 seconds ‘./Payment_Volume_and_Value_Report_000000501C5_2.9_2022_11_14_06_24_54.xml’ 8 seconds ‘./Payment_Volume_and_Value_Report_000000501C5_2022_11_14_06_24_54.xml’ 8 seconds