Monday, January 3, 2011

perl: add a line to a file

For example, insert the line "New line added!!" in line 100 of example.txt:
perl -pi -le 'print "New line added!!" if $. == 100' example.txt


If you want to insert the same line in multiple files, use the following:
perl -pi -le 'print "New line added!!" if $. == 100; close ARGV if eof' *.txt


('close ARGV if eof' is needed to reset the variable '$.' before processing the next file).

No comments: