更新时间:2023-12-03 22:39:52
您可以尝试一下:
$ cat test.txt2015-12-03 17:20:362015-12-03 17:20:392015-12-03 17:21:23[错误] -17-12-2015某事某事测试再次测试$ sed'/\ [错误\] -17-12-2015/,$!d'test.txt[错误] -17-12-2015某事某事测试再次测试$ sed'/\ [错误\] -17-12-2015/,$!d'test.txt>tmpfile&&mv tmpfile test.txt$ cat test.txt[错误] -17-12-2015某事某事测试再次测试
备用:
$ sed -n'/\ [错误\] -17-12-2015/,$ p'test.txt
这意味着仅从与字符串匹配的行开始打印(p),直到文件($)的末尾.-n表示默认情况下不打印行.
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">
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.