简体   繁体   English

在使用sed遇到“[ERROR] -17-12-2015”模式之前,如何删除从第1行到第1行的行?

[英]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.

相关问题 Linux SED 脚本找到匹配模式的第一行并将其删除 - Linux SED script find the 1st line that match pattern and delete it Linux:如何使用sed将从模式开始的行删除到另一个模式 - Linux: How to delete lines starting at a pattern to another pattern using sed 如何使用EGREP搜索行中第一次出现的模式 - How to search for the 1st occurrence of a pattern in a line using EGREP 如何使用Sed删除之前的5行和模式匹配之后的6行? - How to delete 5 lines before and 6 lines after pattern match using Sed? 如何使用 sed 在文件的第一行插入文本? - How do I insert text to the 1st line of a file using sed? 从第n次出现的模式替换到sed的行尾 - Replace from nth occurrence of pattern till the end of line with sed 如何使用 sed 在 FIRST 和 LAST 匹配模式之前插入一行 - How to insert a line before the FIRST and LAST matching pattern using sed 将第一个文件的每一行的逗号前的第一个值与第二个文件逐行匹配 - Match 1st value before comma of each line from first file with second file line by line 我如何使用sed -i命令替换从文件中特定行开始到另一行的字符出现? - How can i use sed -i command to replace the occurance of character starting from particular line in a file till another line? 如何使用sed在匹配模式时删除多行,并停止到第一个空白行? - How to use sed to delete multiple lines when the pattern is matched and stop until the first blank line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM