Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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 use the following perl one line in order to append WORD in the end of the last line in the file

     perl -p -i -e 's/$/WORD/;' file

But it's append the WORD in every line -

How to fix the perl syntax in order to append the WORD only on the last line in the file


  more file

share|improve this question
up vote 4 down vote accepted

You need to detect that you''ve hit the end-of-file using eof:

perl -p -i -e 'eof && s/$/WORD/;'  file
share|improve this answer

Here's an equivalent using sed:

sed -ie '$s/$/WORD/' file
share|improve this answer

Just add the -0 flag:

perl -0pie 's/$/WORD/' file

I'd never use -i without an added extension by the way. If your Perl doesn't do what you intended, your input will be lost. By the way, ; is not a statement terminator in Perl, but a statement separator.

perl -i.orig -0pe 's/$/WORD/' file

Another handy idiom, for testing:

perl -0pe 's/$/WORD/' file | diff file -
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .