2014-04-02 15 views
3

我有一个这样的日志文件。我不希望获取先前在诸如09:28如何根据时间间隔获取这些错误/不匹配字符串

Connected to feeder version 2.1 09:28:30 29/03/2014 Loading AccountEUR 
09:28:30 29/03/2014 Loading AccountJPY 
09:28:30 29/03/2014 Loading AccountINR 
09:28:30 29/03/2014 Loading AccountUSD 
09:28:30 29/03/2014 Loading AccountEUR 
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 AccountEUR 
10:28:30 29/03/2014 Loading AccountGBP 
10:28:30 29/03/2014 Loading AccountINR 
10:28:30 29/03/2014 Loading AccountUSD 
10:28:30 29/03/2014 Loading AccountEUR 
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的价格出现。在此先感谢您的帮助

+0

您可以发布预期的输出吗? – Jotne

+0

你怎么知道09:28线是“先前提取的”?日志文件中是否有标记? –

+0

嗨,基本上我正在写一个监控脚本,每10分钟检查一次这个日志文件,如果有任何错误,那么它会发送这些错误帐户列表。所以1小时后,如果我收到几个错误/不匹配的帐户,我只需要在我的列表中的新帐户。 – user2647888

回答

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 
+0

但是是否有可能每次都自动存储时间戳。 – user2647888

+0

非常有可能。您可以执行'tail -1 file | egrep -o'[0-9] +:[0-9] +''并将其输出存储在某个文件中。 – anubhava

+0

嗨anubhava,非常感谢我在这里有一个轻微的怀疑。我用你建议的命令来获得最新的时间戳,我也得到了它。 tail -1 hopes.txt | egrep -o'[0-9] +:[0-9] +'> tmp.txt 但是当我在这个sed命令中使用这个tmp.txt时sed -n“/^$ ttmp .txt /“'!{s /.*账户余额结余\(。* \)Doesn.t匹配/ \ 1/p; s /.* Account \(。* \)没有设置/ \ 1/p;}'hopes.txt它给了我一切实际上并不是最新的 – user2647888

1

假设你想要最后一个“连接到馈线”行后的行:向后读文件;打印每一行直到你到达指定的模式;重新倒转线条;搜索帐户ID:

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