Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to change the way a few hundred Perl modules end. Currently they end as


However I need to modify this so that they all have this on their last few lines.

# ABSTRACT: table definition

Of course I'm sure I could do this with Perl probably using PPI, but it also seems that I might just be able to sed or awk the end of these files. Can anyone provide any suggestions as to how this might be accomplished?

share|improve this question
up vote 3 down vote accepted

This is actually quite conveniently done with GNU sed, thanks to the combination of $ to act on the last line and -i to modify files in place. This assumes that the 1; is on the very last line, otherwise it would be a lot more complicated.

sed -i -e '$s/^1;$/BEGIN {\
    # VERSION\
# ABSTRACT: table definition/' *.pm

With Perl, since the files are small, just load them fully in memory. This way it's easy to be more flexible, e.g. allow spaces and comments and __DATA__ after 1; (allowing POD is left as an exercise to the reader).

perl -0777 -pe 's/\n\s*1\s*;(?=(?:\s*(?:#.*)?\n)*(?:\s*__(?:DATA|END)__\b)?\Z)/\nBEGIN…definition/' *.pm
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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