我有一个这样的日志文件。我不想获取之前在 09:28 获取的帐户
Connected to feeder version 2.1 09:28:30 29/03/2014 Loading Account 01234567EUR
09:28:30 29/03/2014 Loading Account 0123456755JPY
09:28:30 29/03/2014 Loading Account 0123426567INR
09:28:30 29/03/2014 Loading Account 012345698887USD
09:28:30 29/03/2014 Loading Account 012343422567EUR
09:28:30 29/03/2014 Account 0234456783388KRY not set up
09:28:30 29/03/2014 Account 0234454467888CNH not set up
09:28:30 29/03/2014 Error : Closing Balance of Account 02344567888GBP Doesn't match
Connected to feeder version 2.1 09:28:30 29/03/2014 Loading Account 01234567EUR
10:28:30 29/03/2014 Loading Account 012343356755GBP
10:28:30 29/03/2014 Loading Account 012342654467INR
10:28:30 29/03/2014 Loading Account 01234564498887USD
10:28:30 29/03/2014 Loading Account 01234663422567EUR
10:28:30 29/03/2014 Account 02344567833886KRY not set up
10:28:30 29/03/2014 Account 023445446788866CNH not set up
10:28:30 29/03/2014 Error : Closing Balance of Account 02344567888GBP Doesn't match
现在我使用以下 sed 命令来获取错误帐户
sed -n "
s/.* Closing Balance of Account \(.*\) Doesn't match/\1/p;
s/.* Account \(.*\) not set up/\1/p
"
但是如何只提取新帐户。例如,我不希望在 9.28 提取的帐户再次出现在 10.28 列表中。预先感谢您的帮助
最佳答案
您可以将最近的时间戳存储在单独的文件中并将其传回给 sed:
s='09:28'
sed -n "/^$ts/"'!{s/.* Closing Balance of Account \(.*\) Doesn.t match/\1/p; s/.* Account \(.*\) not set up/\1/p;}' file
02344567833886KRY
023445446788866CNH
02344567888GBP
编辑:要在文件中存储最新的时间戳,请使用:
tail -1 file | egrep -o '^[0-9]+:[0-9]+' > tmp.txt
并使用这个值:
s=$(<tmp.txt)
sed -n "/^$ts/"'!{s/.* Closing Balance of Account \(.*\) Doesn.t match/\1/p; s/.* Account \(.*\) not set up/\1/p;}' ff
关于regex - 如何根据时间间隔获取这些错误/不匹配字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808591/