I need to replace a bit of text in multiple css files, but ony the first occurence.
I've tried replacing with:
perl -pi -e 's/(width:).*;/$1 100%;/' filename.css
But this replaces the value after every occurrence of 'width:' in the file, even though i'm not using the /g modifier. I'm running this on a recent Ubuntu machine.
You can stop the replacement after the first one:
perl -pi -e '$a=1 if(!$a && s/(width:).*;/$1 100%;/);' filename.css
Nobody has addressed an implication of your actual title question. I recommend the same approach as used here, only modified:
perl -pie '!$subbed{$ARGV} and s/(width:).*;/$1 100%;/ and $subbed{$ARGV}++' *.css
The un-dimensioned $ARGV
is the name of the current file. So for each file, you're replacing only the first occurrence. The "glob" *.css
will send multiple files. If you do the scalar switch that other people are suggesting, you will modify only the first occurrence in the first file with that pattern. (Though, perhaps that is what you want.)
If you read in paragraph mode, each file is only one line; hence the s/// without the /g modifier will replace the first instance in each file, then move to the next file:
perl -0777 -pi -e's/(width:).*;/$1 100%;/' *.css
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With