如何根据时间间隔获取这些错误/不匹配字符串

时间:2014-04-02 10:29:24

标签: regex string unix sed awk

我有一个这样的日志文件。我不想获取之前以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列表。在此先感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您可以将最新的时间戳存储在单独的文件中,然后将其传递回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

答案 1 :(得分:1)

假设你想要最后一行&#34;连接到馈线&#34; line:向后读取文件;打印每一行,直到达到指示的图案;重新扭转线条;搜索帐户ID:

tac logfile | 
awk '/^Connected to feeder/ {exit} 1' | 
tac | 
grep -oP '(Closing Balance of )?Account \K\w+(?= not set up| Doesn)'