在遇到模式'[ERROR] -17-12-2015’之前,我需要从第1行删除行直到行
目前我正在尝试下面的命令,但不幸的是它找不到模式本身:

  sed '1,/[ERROR] -17-12-2015/d' errLog

这有什么不对?

其次,上面的脚本也将删除包含模式'[ERROR] -17-12-2015’的行,是否可以在遇到此模式之前仅删除第一行到该行的行?

Sample输入是:


[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表示默认情况下不打印行.

标签: shell, linux, awk, unix

相关文章推荐

添加新评论,含*的栏目为必填