Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • maybe like this: ':x;/PATTERN/{$d;N;s/.*\n//;:z;N;bz};N;bx' to avoid the leading newline Commented Jul 24, 2015 at 0:28
  • Can be rewritten as this sed ':;N;$!b;${s/[^PATTERN]*PATTERN[^\n]*\n//}' Commented Jul 24, 2015 at 13:46
  • @User112638726 - no it can't unless you've got a one char pattern or are 100% certain that none of the chars in your pattern can be matched anywhere else in any other order than the one you look for. But it can be written like: sed -ne'/PATTERN/!d;:n' -e'n;p;bn' - Digital Trauma - the previous little loop won't have any extra/missing char issues, and it doesn't stack like Next either - it repeatedly overwrites the current line w/ the next. Commented Jul 24, 2015 at 14:43
  • @mikeserv Yeah, i only checked with single chars. Yours doesn't work either though, if the patterns not found it deletes everything. Commented Jul 24, 2015 at 14:45
  • @User112638726 - damn. Forgot about that. That is an excellent point. So you gotta kinda burn at bOth ends. sed -ne'/PATTERN/!{H;1h;$!d;x;:p' -ep -e'};n;bn' Commented Jul 24, 2015 at 14:57