我有一个看起来像这样的文本文件:
2017-02-28 18:58:00 (UTC) ServerName: PROBLEM [2018994] bla bla bla bleh bli blu blo [30393](High|Ack: No)
2017-02-28 18:58:00 (UTC) Server Name 2: PROBLEM [2018f34] doo ba di ba doo [32393](High|Ack: Yes)
2017-02-28 18:58:00 (UTC) ServerName 3: PROBLEM [9865994] Link is down [30393](High|Ack: No)
我想过滤此文件,只获取每行的错误消息。 根据我提供的示例,输出将是:
bla bla bla bleh bli blu blo
doo ba di ba doo
Link is down
一件重要的事情:服务器名称有时可以包含空格。 我已经尝试了几个小时的sed,awk,grep,cut,它让我发疯了。任何线索?
答案 0 :(得分:3)
awk 解决方案(针对静态输入格式):
awk -F'[][]' '{ print $3 }' file
输出:
bla bla bla bleh bli blu blo
doo ba di ba doo
Link is down
-F'[][]'
- 将[
和]
视为字段分隔符答案 1 :(得分:1)
动态内容的Perl,假设格式始终相同
perl -lne 'print $& if /]\K.*(?=\[)/' file
输入
2017-02-28 18:58:00 (UTC) ServerName: PROBLEM [2018994] bla bla bla bleh bli blu blo [30393](High|Ack: No)
2017-02-28 18:58:00 (UTC) Server Name 2: PROBLEM [2018f34] doo ba di ba doo [32393](High|Ack: Yes)
2017-02-28 18:58:00 (UTC) ServerName 3: PROBLEM [9865994] Link is down [30393](High|Ack: No)
2017-02-28 18:58:00 (UTC) ServerName 3: PROBLEM [9865994] 2017-02-28 18:58:00 (UTC) ServerName 3: PROBLEM [9865994] Link is down [30393](High|Ack: No) [30393](High|Ack: No)
输出
bla bla bla bleh bli blu blo
doo ba di ba doo
Link is down
2017-02-28 18:58:00 (UTC) ServerName 3: PROBLEM [9865994] Link is down [30393](High|Ack: No)
答案 2 :(得分:1)
sed 's/[^]]*.\(.*\)\[.*/\1/' file
答案 3 :(得分:0)
使用my_total = Byte.objects.count()
sed