Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl inline edit 2

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
US
Code:
        open (FS, "/etc/vfstab") or die "Can't open /etc/vfstab: $!\n";
        while ($line=<FS>) {
                ($devmnt, $devfsck, $mntpt, $fstype, $fsckpass, $mntboot, $mntopt) = split(/\s+/,$line,7);
                if ($devmnt =~ /datadg/) {
                        $chardev = substr($devmnt, 0, 1);
                                if ($chardev =~ /#/) {
                                       perl -pi.bkup -e 's/^#//' /etc/vfstab;
                                        printf "%30s     %30s     %30s    %5s     %2s     %3s     %1s\n", $devmnt, $devfsck, $mntpt, $fstype, $fsckpass, $mntboot, $mntopt;
                                }
                }
        }
I need to do inline editing so if the first character of the match is a '#' then I want to remove the # but only for that line while it loops through the file.

Example:
this:
#/dev/vx/dsk/datadg/data01 /dev/vx/dsk/datadg/data01 /data01 vxfs 3 yes -
should be:
/dev/vx/dsk/datadg/data01 /dev/vx/dsk/datadg/data01 /data01 vxfs 3 yes -

Doing the perl -pi -e isn't working, plus I don't want to have to put /etc/vfstab at the end.

Thanks.
 
If you have a look at the -i section of man perlrun it includes some sample code that describes exactly how that option could be implemented manually; copy that I guess and modify it to your requirements?

Annihilannic.
 
If you are using a script invoke the inplace editor with the inplace edit variable:

Code:
$^I = '.bac';

A short example (from the perl FAQs):

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $filename = 'some_file.txt';
my $find = 'this';
my $replace = 'that';

{
   local @ARGV = ($filename);
   local $^I = '.bac';#<-- invokes inplace editing
   while( <> ){
      if( s/$find/$replace/ig ) {
         print;
      }
      else {
         print;
      }
   }
}

change '.bac' to whatever suits your needs. If you are on Unix or Linux you can pass it an empty value and no backup copy of the original file will be saved. But Windows requires it has a non-false value.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That works almost like I need except it is removing the # on all lines but I want it to remove the # in front of the lines that match my disk group only. Is there a way to do that?

Thanks.
 
Maybe:

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $filename = '/etc/vfstab';

{
   local @ARGV = ($filename);
   local $^I = '.bac';#<-- invokes inplace editing
   while( <> ){
      if( m/^#/ && m|/datadg/|) {
         s/^#//;
         print; 
      }
      else {
         print;
      }
   }
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top