Sed delete blank lines upto first pattern match

Hi
Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example:

Input

output:

I have got it to work within a range of two patterns with the following:

sed '/1/,/pattern/{/[1]*$/d}'

The problem is that it will carry on in the file and if there is another match it will act on that. I only want it to act once.


  1. [:blank:] ↩︎

Have a go with this:

sed '/pattern/,$ b; /^$/d;' 
  

This looks for the first occurrence of 'pattern' and from there skips the delete portion of the script, so only blank lines before the pattern are deleted.
It assumes your 'blank lines' are empty lines (no whitespace). If you truly have whitespace, then you can replace the delete with something like you've been using.

as second solution you can try this :wink:

# sed '/pattern/,$!{/^$/d}' file

regards
ygemici