我需要在遇到模式“[ERROR] -17-12-2015”之前删除从第一行到该行的行
目前我正在尝试以下命令,但不幸的是它没有找到模式本身:
sed '1,/[ERROR] -17-12-2015/d' errLog
这里有什么问题吗?
其次,上面的脚本还将删除包含模式 '[ERROR] -17-12-2015' 的行,是否可以仅删除从第一行到遇到此模式之前的行?
示例输入是:
[ERROR] -09-11-2015 05:22:17 : : XMLrequest failed: You do not have access
[ERROR] -09-11-2015 05:22:18 : : XMLrequest failed: You do not have access to period 2015/12, XMLrequest received: <?xml version="1.0" encoding="UTF-8"?>
<MatchingRequest version="12.0"><StartBackground>
[ERROR] -17-12-2015 05:22:18 : : XMLrequest failed: You do not have access
[ERROR] -17-12-2015 05:22:18 : : XMLrequest failed: You do not have access
[ERROR] -17-12-2015 05:22:18 : : XMLrequest failed: You do not have access
[ERROR] -09-11-2015 05:22:18 : : XMLrequest failed: You do not have access , XMLrequest received: <?xml version="1.0" encoding="UTF-8"?>
<MatchingRequest version="12.0">
预期输出:
[ERROR] -17-12-2015 05:22:18 : : XMLrequest failed: You do not have access
[ERROR] -17-12-2015 05:22:18 : : XMLrequest failed: You do not have access
[ERROR] -17-12-2015 05:22:18 : : XMLrequest failed: You do not have access
[ERROR] -09-11-2015 05:22:18 : : XMLrequest failed: You do not have access , XMLrequest received: <?xml version="1.0" encoding="UTF-8"?>
<MatchingRequest version="12.0">
你可以尝试一下:
$ cat test.txt
2015-12-03 17:20:36
2015-12-03 17:20:39
2015-12-03 17:21:23
[ERROR] -17-12-2015 something something
testing
testing again
$ sed '/\[ERROR\] -17-12-2015/,$!d' test.txt
[ERROR] -17-12-2015 something something
testing
testing again
$ sed '/\[ERROR\] -17-12-2015/,$!d' test.txt > tmpfile && mv tmpfile test.txt
$ cat test.txt
[ERROR] -17-12-2015 something something
testing
testing again
备用:
$ sed -n '/\[ERROR\] -17-12-2015/,$p' test.txt
这意味着仅从与字符串匹配的行开始打印 (p) 到文件末尾 ($)。 -n 表示默认不打印行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)