How to add a line in every file with perl commandline?

Hi, i want to add a line at the beginning of every file in a directory.
perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1' *.reset

But this command is updating only the first file in the directory. I think this is because $. is not resetting for next file.

How to modify all the files.

If you have GNU sed available you could do

sed -i '1i\
#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset' *
1 Like
perl -i.bkp -p -e 'BEGIN { $p="" } if( $ARGV != p ) { print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n"; $p=$ARGV } ' *.reset
1 Like

Bad practice(?) but seems to work:

perl -i.bkp -p -e 'print "#include /verif/pdd1/exu/sxs/6sTest/reset/dfu/top_level.reset\n" if $. == 1; $.=0 if eof' *.reset