我有一个日志(由 Unison 文件同步生成),如下所示:
UNISON 2.51.3 (OCAML 4.11.1) started propagating changes at 15:25:10.21 on 27 Feb 2023
[BGN] Copying TestFolder/01/TestFile01.pdf from //10.0.0.2//home/testsync to /home/testsync
[END] Copying TestFolder/01/TestFile01.pdf
[BGN] Copying TestFolder/01/TestFile02.pdf from //10.0.0.2//home/testsync to /home/testsync
[END] Copying TestFolder/01/TestFile02.pdf
[BGN] Copying TestFolder/01/TestFile03.xlsx from //10.0.0.2//home/testsync to /home/testsync
Shortcut: copied /home/testsync/TestFolder/01/TestFile03.xlsx from local file /home/testsync/TestFolder/02/TestFile03.xlsx
[END] Copying TestFolder/01/TestFile03.xlsx
[BGN] Updating file TestFolder/01/TestFile04.jpg from //10.0.0.2//home/testsync to /home/testsync
[END] Updating file TestFolder/01/TestFile04.jpg
[BGN] Deleting /home/testsync/TestFolder/02/TestFile03.xlsx from /home/testsync
UNISON 2.51.3 (OCAML 4.11.1) finished propagating changes at 15:25:46.24 on 27 Feb 2023
我想得到与此类似的结果:
27 Feb 2023 15:25:10.21 Copying TestFolder/01/TestFile01.pdf from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Copying TestFolder/01/TestFile02.pdf from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Copying TestFolder/01/TestFile03.xlsx from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Shortcut: copied /home/testsync/TestFolder/01/TestFile03.xlsx from local file /home/testsync/TestFolder/02/TestFile03.xlsx
27 Feb 2023 15:25:10.21 Updating file TestFolder/01/TestFile04.jpg from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Deleting /home/testsync/TestFolder/02/TestFile03.xlsx from /home/testsync
换句话说,我需要记住 row 上显示的日期和时间started propagating changes
,并打印回rows 之前的[BGN]
所有行。Shortcut
finished propagating changes
我如何在 bash 脚本中做到这一点?
cat
我已经使用,grep
和串联进行了一些测试awk
,但无法实现我需要的输出。我需要使用 Bash 脚本,因为日志格式是 Unison 文件同步器程序的默认日志,但我想跟踪同步所做的所有修改,并且我需要在程序运行后执行此操作。
感谢埃德·莫顿的回复解决了我的问题。我只修改了一点 awk 语法,因为输入日志中可以有多个部分(我忘记告诉这一点)。我的最终 AWK 语法如下:
sub(/.*started propagating changes at /,"") {
mthNr = index(" JanFebMarAprMayJunJulAugSepOctNovDec",$4) / 3
ts = sprintf("%04d-%02d-%02dT%s", $5, mthNr, $3, $1)
next
}
sub(/^\[BGN] /,"") || /^Shortcut/ {
print ts, $0
}
答案1
使用任何 awk:
$ cat tst.awk
sub(/.*started propagating changes at /,"") {
ts = $3 FS $4 FS $5 FS $1
next
}
sub(/^\[BGN] /,"") || /^Shortcut/ {
print ts, $0
}
/finished propagating/ {
exit
}
$ awk -f tst.awk file
27 Feb 2023 15:25:10.21 Copying TestFolder/01/TestFile01.pdf from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Copying TestFolder/01/TestFile02.pdf from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Copying TestFolder/01/TestFile03.xlsx from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Shortcut: copied /home/testsync/TestFolder/01/TestFile03.xlsx from local file /home/testsync/TestFolder/02/TestFile03.xlsx
27 Feb 2023 15:25:10.21 Updating file TestFolder/01/TestFile04.jpg from //10.0.0.2//home/testsync to /home/testsync
27 Feb 2023 15:25:10.21 Deleting /home/testsync/TestFolder/02/TestFile03.xlsx from /home/testsync
考虑将时间戳更改为可排序且通常更有用的内容,例如ISO 8601不过(酌情附加您的时区信息):
$ cat tst.awk
sub(/.*started propagating changes at /,"") {
mthNr = index(" JanFebMarAprMayJunJulAugSepOctNovDec",$4) / 3
ts = sprintf("%04d-%02d-%02dT%s", $5, mthNr, $3, $1)
next
}
sub(/^\[BGN] /,"") || /^Shortcut/ {
print ts, $0
}
/finished propagating/ {
exit
}
$ awk -f tst.awk file
2023-02-27T15:25:10.21 Copying TestFolder/01/TestFile01.pdf from //10.0.0.2//home/testsync to /home/testsync
2023-02-27T15:25:10.21 Copying TestFolder/01/TestFile02.pdf from //10.0.0.2//home/testsync to /home/testsync
2023-02-27T15:25:10.21 Copying TestFolder/01/TestFile03.xlsx from //10.0.0.2//home/testsync to /home/testsync
2023-02-27T15:25:10.21 Shortcut: copied /home/testsync/TestFolder/01/TestFile03.xlsx from local file /home/testsync/TestFolder/02/TestFile03.xlsx
2023-02-27T15:25:10.21 Updating file TestFolder/01/TestFile04.jpg from //10.0.0.2//home/testsync to /home/testsync
2023-02-27T15:25:10.21 Deleting /home/testsync/TestFolder/02/TestFile03.xlsx from /home/testsync