问题描述
在遇到模式"[ERROR] -17-12-2015"之前,我需要从第一行删除所有行目前,我正在尝试使用以下命令,但不幸的是它找不到该模式本身:
sed'1,/[错误] -17-12-2015/d'errLog
这是怎么了?
第二,上面的脚本还将删除包含模式'[ERROR] -17-12-2015'的行,是否有可能在遇到该模式之前仅从第一行删除该行?>
样本输入为:
[错误] -09-11-2015 05:22:17::XMLrequest失败:您无权访问[错误] -09-11-2015 05:22:18::XMLrequest失败:您无权访问期间2015/12,收到XMLrequest:<?xml version ="1.0" encoding ="UTF-8"?>< MatchingRequest version ="12.0">< StartBackground>[错误] -17-12-2015 05:22:18::XMLrequest失败:您无权访问[错误] -17-12-2015 05:22:18::XMLrequest失败:您无权访问[错误] -17-12-2015 05:22:18::XMLrequest失败:您无权访问[错误] -09-11-2015 05:22:18::XMLrequest失败:您没有访问权限,收到XMLrequest:<?xml version ="1.0" encoding ="UTF-8"?>< MatchingRequest version ="12.0">
预期输出:
[错误] -17-12-2015 05:22:18::XMLrequest失败:您无权访问[错误] -17-12-2015 05:22:18::XMLrequest失败:您无权访问[错误] -17-12-2015 05:22:18::XMLrequest失败:您无权访问[错误] -09-11-2015 05:22:18::XMLrequest失败:您没有访问权限,收到XMLrequest:<?xml version ="1.0" encoding ="UTF-8"?>< MatchingRequest version ="12.0">
您可以尝试一下:
$ 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.
这篇关于在使用sed遇到模式[[ERROR] -17-12-2015]之前,如何删除从第一行开始的行,直到该行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!