3

I am trying to add a line to many perl files at line 4 using perl liner. I am using :

 perl -pi -le 'print "     cell_type = pad;" if $. ==4' *.cell.plt

But this is only changing the first file in my directory, not all of them. How do I insert the line in all files at once. I tried several ways but it always fails. Please help. Thanks.

0

1 Answer 1

7

You're only reading from one file handle, so there's only one line 4. Fortunately, there is a way of resetting $..

perl -i -ple'
    print "     cell_type = pad;" if $. == 4;
    close ARGV if eof;
' *.cell.plt

(Note that eof is different than eof().)

Alternatively, you can execute perl for each file

find -maxdepth 1 -name '*.cell.plt' -type f -exec \
   perl -i -ple'print "     cell_type = pad;" if $. == 4' {} \;
Sign up to request clarification or add additional context in comments.

2 Comments

Thought it might be something like that, but haven't done enough perl one-lining to be sure :)
@Sobrique, Has nothing to do with "one-lining". I have lots of programs that aren't one-liners that use <>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.