Reputation: 105
I have lots of Perl scripts that need to be updated to a consistent version required and have various versions and flags currently. All of these statements lie in the first line of the script. I have not had much luck here, obviously trying to use sed but is there something better?
Example line that needs to be edited
#!/opt/local/bin/perl5
I tried a bash script as follows, but it does not work for me.
#!/bin/bash
echo "Changing Perl headers in the following..."
for file in $(ls $1*.pl)
do
echo $file
sed 's/#!\/opt\/local/bin\/perl5/#![PERL5]/' $file > /dev/null
# Perl Version: PERL5
done
Any help from someone experienced with sed would be great! Thanks!
Upvotes: 2
Views: 1776
Reputation: 11
Suppose if you want to change perl5 to perl6 Try this: perl -pi -e 's/perl5/perl6/' $file
Upvotes: 1
Reputation: 67908
To edit files with perl, you'd use the -pi
switches. Since you only wanted to change the first line, I was considering a way to do so without causing the regex to change anything else in the file. This little hack will allow that. Not sure if it's worth the effort, since shebang-like strings should not really be found elsewhere.
perl -pi.bak -e '
$x //= s|^#!/opt/local/bin/perl5|#![PERL5]|;
undef $x if eof;' *.pl
The //=
operator will not attempt to execute the RHS if $x
is already defined, so this will in effect only perform one change. The addition of undef $x
at eof
will reset the variable for the next file, causing each file in the argument list to be altered once.
You may not want backups, in which case you'd remove .bak
from the -i
switch.
Upvotes: 1
Reputation: 28029
I'm not exactly sure what it is you want. If you're wanting to edit all of the files in $DIR
so that their first line is #![PERL5]
instead of #!/opt/local/bin/perl5
, this will do that:
sed --in-place '1s:^#!/opt/local/bin/perl5:#![PERL5]:' "$DIR"/*.pl
Upvotes: 1
Reputation: 164829
Use Perl to fix your Perl scripts. You can use braces for the regexes to avoid leaning toothpick syndrome.
perl -i -pe 's{^#!/opt/local/bin/perl5}{#!/some/new/path}' *.pl
-i
and -p
are used to edit files in place, kinda like sed. See perlrun for details.
Upvotes: 4