I’m trying to use sed
to print all lines until but excluding a specific pattern. I don’t understand why the following doesn’t work:
sed '/PATTERN/{d;q}' file
According to my understanding of sed scripting, this expression should cause the following:
- When a line matches
/PATTERN/
, execute the group consisting of commands tod
elete the pattern space (= the current line)q
uit after printing the current pattern space
In isolation, both /PATTERN/d
and /PATTERN/q
work; that is, d
deletes the offending line, and q
causes sed
to terminate but after printing the line, as documented. But grouping the two operations together in a block seemingly causes the q
to be ignored.
I know that I can use Q
instead of {d;q}
as a GNU extension (and this works as expected!) but I’m interested in understanding why the above doesn’t work, and in what way I am misinterpreting the documentation.
My actual use-case is (only slightly) more complex, since the first line of the file actually matches the pattern, and I’m skipping that (after doing some replacement):
sed -e '1{s/>21/>chr21/; n}' -e '/>/{d;q}' in.fasta >out.fasta
But the above, simplified case exhibits the same behaviour.