[英]How to delete the lines starting from the 1st line till line before encountering the pattern '[ERROR] -17-12-2015' using sed?
I need to delete lines from the 1st line till line before encountering the pattern '[ERROR] -17-12-2015' Currently I am trying the below command but unfortunately it does not find the pattern itself: 我需要在遇到模式'[错误] -17-12-2015'之前删除第1行到第7行的行。目前我正在尝试以下命令,但不幸的是它找不到模式本身:
sed '1,/[ERROR] -17-12-2015/d' errLog
What is wrong here? 这有什么不对?
Secondly, the above script will also delete the line containing pattern '[ERROR] -17-12-2015' , is it possible to delete only the lines from the first line to the line before encountering this pattern ? 其次,上面的脚本也将删除包含模式'[ERROR] -17-12-2015'的行,是否可以在遇到此模式之前仅删除第一行到该行的行?
The Sample input is: 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">
Expected Output: 预期产出:
[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">
You could give this a shot: 你可以试一试:
$ 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
Alternate: 备用:
$ sed -n '/\[ERROR\] -17-12-2015/,$p' test.txt
That means only begin print (p) from the line that matches the string through the end of file ($). 这意味着只从匹配字符串到文件末尾($)的行开始打印(p)。 -n means don't print lines by default. -n表示默认情况下不打印行。
Here is a non-regex based solution that doesn't require any escaping etc, using awk: 这是一个基于非正则表达式的解决方案,使用awk不需要任何转义等:
awk '!p && index($0, "[ERROR] -17-12-2015")==1{p=1} p' file
[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">
awk
to the rescue! awk
来救援!
$ awk '/\[ERROR\] -17-12-2015/,0' filename
prints from pattern to end of file. 从模式打印到文件末尾。
The right way to do this is simply to set a flag when you find a line matching your regexp and then print when the flag is set: 正确的方法是在找到与正则表达式匹配的行时设置标志,然后在设置标志时进行打印:
$ awk '/\[ERROR\] -17-12-2015/{f=1} f' file
[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">
Remember - sed is for simple subsitutions on individual lines, that is all. 请记住 - sed是针对单个行的简单替换,即所有。 That's not what this problem is so using sed would be the wrong approach, it's a job for awk. 这不是什么问题所以使用sed将是错误的方法,这是awk的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.