2

I want to add the PATH - /usr/local/bin/perl befpore string in the file by perl command line ( the perl command line must be in my bash script ) , I have solaris machine

the target to add - /usr/local/bin/perl before the line $APIDIR/scan.pl in the file and after the char - "

please advice how to add the PATH - /usr/local/bin/perl before $APIDIR/scan.pl so finally I will get

the new line's -

      my $APIDIR="/usr/local/cp/api";           remark -  this line shouldn’t be change
      my $script="/usr/local/bin/perl   $APIDIR/scan.pl";  

in place of

     my $APIDIR="/usr/local/cp/api";
     my $script="$APIDIR/scan.pl";

I try the following but its not change the line:

  [root@machine1a /var/tmp]#  perl -p -i -e 's/\/usr\/local\/bin\/perl \$APIDIR\/scan.pl/\$APIDIR\/scan.pl/g' file




  [root@machine1a /var/tmp]# more file
  my $APIDIR="/usr/local/cp/api";
  my $script="$APIDIR/scan.pl";

2 Answers 2

1

You can do this with sed e.g.

sed 's|$APIDIR/scan.pl| /usr/local/bin/perl   $APIDIR/scan.pl|' file >file.new

or with perl

perl -p -i -e  's|$APIDIR/scan.pl| /usr/local/bin/perl   $APIDIR/scan.pl|' file
0

So you want to replace $APIDIR with /usr/local/bin/perl $APIDIR ? If so, your regex is backward. You want something like:

s/\$APIDIR\/\/usr\/local\/bin\/perl $APIDIR/

So try:

perl -p -i -e 's#\$APIDIR#\/usr\/local\/bin\/perl \$APIDIR#g' file

I hope this is what you were after-

8
  • sorry but its not work - perl -p -i -e 's/\$APIDIR//usr/local/bin/perl \$APIDIR/g' file Bareword found where operator expected at -e line 1, near "s/\$APIDIR//usr" Bareword found where operator expected at -e line 1, near "/bin/perl" (Missing operator before perl?)
    – Eytan
    May 22, 2012 at 5:05
  • My answer appears to have been modified during submission. The "//" between $APDIR and usr should be replaced with "\/".
    – rnxrx
    May 22, 2012 at 5:09
  • still the same problem - perl -p -i -e 's/\$APIDIR/\/usr/local/bin/perl \$APIDIR/g' file Bareword found where operator expected at -e line 1, near "/bin/perl" (Missing operator before perl?) Backslash found where operator expected at -e line 1, near "perl \"
    – Eytan
    May 22, 2012 at 5:16
  • You have slash in your string. Use other sparator. Example: perl -p -i -e 's:\$APIDIR:/usr/local/bin/perl \$APIDIR:g' file
    – 0xFF
    May 22, 2012 at 5:19
  • the target is to add the PATH only before $APIDIR/scan.pl - so its not good to add the PATH before $APIDIR because I have allot of string as $APIDIR in the file see my example ( I edit my quastion
    – Eytan
    May 22, 2012 at 5:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .