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

标签 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 列表中。预先感谢您的帮助

最佳答案

您可以将最近的时间戳存储在单独的文件中并将其传回给 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/

相关文章:

python - 键和多行 python 值

php - 用 preg_replace 替换 ereg_replace

c++ - 在 Raspberry Pi 上编译 JUMPCoin 钱包时出错 (leveldb/libmemenv.a : error adding symbols:)

c - 运行命令后 GDB 立即退出回到终端(即使设置了断点)

algorithm - 平衡绳的串联复杂度是多少?

linux - Unix - 文件名开头的空格

python - 在 re.split 中使用管道会导致发生额外的拆分

java - 从解析的 Word 文档返回文本的正则表达式

Java 正则表达式字符串 : Check if ip:port string contains a valid IPv4 or DNS address

python - 如何使用正则表达式只替换括号内的内容?