How to delete the lines starting from the 1st line till line before encountering the pattern '[ERROR] -17-12-2015' using sed?

▼魔方 西西 提交于 2021-01-27 11:43:29

问题


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:

  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 ?

The Sample input is:

[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">

回答1:


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 ($). -n means don't print lines by default.




回答2:


Here is a non-regex based solution that doesn't require any escaping etc, using 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">



回答3:


awk to the rescue!

$ awk '/\[ERROR\] -17-12-2015/,0' filename

prints from pattern to end of file.




回答4:


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. That's not what this problem is so using sed would be the wrong approach, it's a job for awk.



来源:https://stackoverflow.com/questions/34377793/how-to-delete-the-lines-starting-from-the-1st-line-till-line-before-encountering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!