I wanted to source the system xinitrc except the last paragraph in my local xinitrc, so I used

    % sed -n 'H
            /^exec ./{
            }' /etc/X11/xinit/xinitrc

That is, when ^exec is encountered, I print everything up to that point except the exec line and the above part of its paragraph.

My question is: could this have been done more simply? Could it, for instance, have been done without using the hold space? I tried using N, but couldn't make it work as expected.

| improve this question | | | | |

In sed, "and the above part of its paragraph" basically means yes. If you want to decide whether or not to print lines based on the content of later lines, you're pretty much gonna have to use the buffer, unless you can restructure your paradigm.

What defines the "paragraph"? Is it consistent? perl and awk will let you define fairly arbitrary record separators. You might try something like this:

$: awk 'BEGIN{ RS="if " }
        /exec/{ exit; }
        { print }
       ' /etc/X11/xinit/xinitrc

I recommend studying your data for a better separator, though.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.