3

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">
user2077648
  • 951
  • 7
  • 27
  • 42
  • 1
    `[` and `]` are regex metacharacters, you;d have to escape them for them to be taken literally. Post sample input and expected output if you want more help. – Ed Morton Dec 20 '15 at 03:57
  • I always find it interesting when people write negative requirements like `I need to delete lines..` instead of the positive `I need to print lines...` and I find it much more common when people assume they're going to use sed since it prints lines by default and you need to write code to stop it from doing that. Funny how the assumed choice of tool can color your requirements and make them harder to figure out if you're not starting out with that same mindset. I still haven't figured out what your requirement actually means! – Ed Morton Dec 20 '15 at 16:04
  • Eureka - I just figured it out! You just want to print from the first line containing `[ERROR] -17-12-2015` to the end of the file, right? It's much easier to understand when stated that way than "delete from the start of the file to the line before the first line containing `[ERROR] -17-12-2015`"! – Ed Morton Dec 20 '15 at 16:12

4 Answers4

3

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.

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
3

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">
anubhava
  • 761,203
  • 64
  • 569
  • 643
3

awk to the rescue!

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

prints from pattern to end of file.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • 1
    @Basilevs: It is the - mistaken - _complement_ of what the OP asked for. The second condition, `0`, will by design never be true and thus match through the end of the input file (the equivalent of `$` in `sed`). – mklement0 Dec 20 '15 at 04:47
1

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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    sets a flag when it finds the regexp, prints every line on and after the flag is set. Get the book Effective Awk Programming, 4th Edition, by Arnold Robbins. – Ed Morton Dec 22 '15 at 18:37