469,304 Members | 2,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Deleting lines in a file

I have many files in a dirctory in which I need to make the common subsitution.

I need to delete all lines between two matched patterns


I need to match a line having words " chkstats to capture " starting from this line

(Say it occurs at line 40) to another line "Failure to pull stats" (which may come in

line no 100) and there is close brace('}") at the next line which also needs to be removed.


In short all lines from 40 to 100 must be deleted in the file.(Including the symbol "}")

I tried the same I can match the pattern in the file but I dont know how to remove the lines..between the matched patterns..Can anyone tell me how to do the same.


Expand|Select|Wrap|Line Numbers
  1. $dirname="D:\\GTP\\temp";
  2. open(FH3, ">filelist.txt") || die "Can't create the report file";
  3. unless (opendir (DIR,"$dirname")){
  4.     print "Can't open the directory $dirname \n";
  5.     exit;
  6. }
  7. while ($file = readdir(DIR)){
  8.     print FH3 "$file\n";
  9. }
  10. close(FH3);
  11.  
  12. open (FH, qq*<filelist.txt*) || die "Can't find the filelist.txt";
  13. while(chomp( $mfile=<FH>)) {
  14.     if (-e $mfile && $mfile=~/PC/) {
  15.         print "$mfile\n";
  16.         $mbak_file=$mfile. ".bak";
  17.         system("copy $mfile $mbak_file");
  18.         open (FH1,"<$mbak_file");
  19.         open (FH2,">$mfile");
  20.         while (<FH1>) {
  21.             #Pattern matching and deletion should be done here
  22.             print FH2 $_;
  23.         }
  24.  
  25.         close (FH1);
  26.  
  27.         close (FH2);
  28.         system("del $mbak_file");
  29.  
  30.     } else {
  31.         print "\nCan't find the file $mfile. Please verify in the filelist.txt";
  32.     }
  33. }
  34.  
  35. close (FH);
  36.  
Jun 18 '07 #1
10 4283
KevinADC
4,059 Expert 2GB
post what the real data looks like you are wanting to delete the lines from.
Jun 18 '07 #2
miller
1,089 Expert 1GB
Something like the following should work. At the very least you can use the logic as a model.

Expand|Select|Wrap|Line Numbers
  1. use File::Copy qw(copy);
  2.  
  3. use strict;
  4.  
  5. my $reportfile = 'filelist.txt';
  6. my $dirname = "D:/GTP/temp";
  7.  
  8. opendir(DIR, $dirname) or die "Can't open $dirname: $!";
  9.  
  10. open(REPORTFH, "> $reportfile") or die "Can't open $reportfile: $!";
  11.  
  12. while (readdir(DIR)) {
  13.     my $file = $_;
  14.     print REPORTFH "$file\n";
  15.  
  16.     if (! -e "$dirname/$file") {
  17.         print "Can't find the file $file. Please verify in the $reportfile\n";
  18.     } elsif ($file =~ /PC/) {
  19.         # Truncate Files:
  20.         # Start: / chkstats to capture /
  21.         # End: /Failure to pull stats/ + next line /}/
  22.  
  23.         my $source = "$dirname/$file.bak";
  24.         my $target = "$dirname/$file";
  25.  
  26.         copy($target, $source) or die "Copy failed: $!";
  27.  
  28.         open(IN, $source) or die "Can't open $source: $!";
  29.         open(OUT, "> $target") or die "Can't open $target: $!";
  30.  
  31.         my $truncateMode = 0;
  32.  
  33.         while (IN) {
  34.             if (! $truncateMode) {
  35.                 if (/ chkstats to capture /) {
  36.                     $truncateMode = 1;
  37.                 } else {
  38.                     print OUT $_;
  39.                 }
  40.             } else {
  41.                 if (/Failure to pull stats/) {
  42.                     $_ = <IN>; # Slurp in extra line for }
  43.                     $truncateMode = 0;
  44.                 } else {
  45.                     # During Truncation, Ignore.
  46.                 }
  47.             }
  48.         }
  49.  
  50.         close (IN);
  51.         close (OUT);
  52.  
  53.         unlink $source or die "Can't unlink $source: $!";
  54.     }
  55. }
  56.  
  57. close(REPORTFH);
  58.  
- Miller
Attached Files
File Type: txt scratch.txt (1.2 KB, 385 views)
Jun 18 '07 #3
KevinADC
4,059 Expert 2GB
A different approach using perls inplace editor:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use diagnostics;
  4.  
  5. my $dirname = 'D:/GTP/temp';
  6. chdir($dirname) or die "Can't chdir to $dirname: $!";
  7.  
  8. opendir (DIR, $dirname) or die "Can't open $dirname: $!";
  9. my @filelist = grep { (-e) && (/PC/) } readdir(DIR);
  10. close(DIR);
  11. {
  12.    local @ARGV = @filelist;
  13.    local $^I = '.bak';
  14.    OUTTER: while(<>) {
  15.       if (/chkstats to capture/) {
  16.          while(<>) {
  17.             if (/Failure to pull stats/) {
  18.                readline(<>);# skip next line with closing brace '}' 
  19.                next OUTTER; # restart loop
  20.             }
  21.          }
  22.       }  
  23.       print;
  24.    }
  25. }
  26. print "Finished";
only thing it doesn't do is delete the backup copies but that could be done easy enough.
Jun 18 '07 #4
Thanks Kevin and Miller It really helped me a lot in learning more in Perl.

Miller In the code You have given, I get the following errors

D:\GTP\temp>perl clear.pl
Global symbol "$dir" requires explicit package name at clear.pl line 16.
Global symbol "$dir" requires explicit package name at clear.pl line 23.
Global symbol "$dir" requires explicit package name at clear.pl line 24.
Execution of clear.pl aborted due to compilation errors.

when I commented the strict portion It did not given any complilation error but the there is no contents in reportfile generated.
while (readdir(DIR)) {
print $_;//I tried to print the filenames it also didnot work..
my $file = $_;

print REPORTFH "$file\n";

Not sure what is the reason for this....

Kevin,
I tried to execute the code it works as expected.. but I get the following error also..


D:\GTP\temp>perl remove.pl
readline() on unopened filehandle } (#1)
(W unopened) An I/O operation was attempted on a filehandle that was
never initialized. You need to do an open(), a sysopen(), or a socket()
call, or call a constructor from the FileHandle package.


Not sure why it comes...

Kevin I cont understand the logic used in below part of code..(While Loop and how you skip that } )

OUTTER: while(<>) {
if (/chkstats script to capture /) {
while(<>) {
if (/Failure to pull stats/) {
readline(<>);# skip next line with closing brace '}'
next OUTTER; # restart loop
}
}
}
print;
}
}
print "Finished";

can you please explain the same
Jun 19 '07 #5
KevinADC
4,059 Expert 2GB
In Millers code, change the instances of $dir noted in the errors below to $dirname.

Global symbol "$dir" requires explicit package name at clear.pl line 16.
Global symbol "$dir" requires explicit package name at clear.pl line 23.
Global symbol "$dir" requires explicit package name at clear.pl line 24


.
Jun 19 '07 #6
KevinADC
4,059 Expert 2GB
Kevin,
I tried to execute the code it works as expected.. but I get the following error also..


D:\GTP\temp>perl remove.pl
readline() on unopened filehandle } (#1)
(W unopened) An I/O operation was attempted on a filehandle that was
never initialized. You need to do an open(), a sysopen(), or a socket()
call, or call a constructor from the FileHandle package.


Not sure why it comes...
It appears that when the script finds the pattern:

if (/Failure to pull stats/) {

it's the last line of the file? This is just a warning though and not an error. It may not have any impact on how the script works, it's just warning you there might be a problem.

Kevin I cont understand the logic used in below part of code..(While Loop and how you skip that } )

OUTTER: while(<>) {
if (/chkstats script to capture /) {
while(<>) {
if (/Failure to pull stats/) {
readline(<>);# skip next line with closing brace '}'
next OUTTER; # restart loop
}
}
}
print;
}
}
print "Finished";

can you please explain the same
When the pattern /Failure to pull stats/ is found in the file, I instructed the script to read one more line from the file: readline(<>); The line is read but just discarded, which has the affect of skipping the line after the pattern, which is where you said the closing brace '}' was. I hope that makes sense.
Jun 19 '07 #7
miller
1,089 Expert 1GB
In Millers code, change the instances of $dir noted in the errors below to $dirname.

Global symbol "$dir" requires explicit package name at clear.pl line 16.
Global symbol "$dir" requires explicit package name at clear.pl line 23.
Global symbol "$dir" requires explicit package name at clear.pl line 24
.
I've so editted my original code and also included an attached copy.

- Miller
Jun 19 '07 #8
Thanks Kevin for the Info.
Still I have few more doubts can you clear the same.

1) local $^I = '.bak';
In the above line how it creates a bak file(You have never used copy command).

2) OUTTER: while(<>) {
if (/chkstats script to capture /) {
while(<>) {
In the outer while loop what will be read(It must be each line on the file) and in the Inner while loop what will be read??

3) Is the "Print" statement is used to write the contents needed back to file.
Jun 19 '07 #9
KevinADC
4,059 Expert 2GB
Thanks Kevin for the Info.
Still I have few more doubts can you clear the same.

1) local $^I = '.bak';
In the above line how it creates a bak file(You have never used copy command).



$^I is a special variable. It tells perl you want to edit your files "inplace". Perl knows to make a backup copy using .bak (or anything you define the variable as) to create a backup copy with that extension before editing the file.

2) OUTTER: while(<>) {
if (/chkstats script to capture /) {
while(<>) {
In the outer while loop what will be read(It must be each line on the file) and in the Inner while loop what will be read??

Each line of the file is read in both loops.

3) Is the "Print" statement is used to write the contents needed back to file.

Yes, that is correct.


Some reading that should help explain things:

perldoc perlop I/O operators
Jun 19 '07 #10
ajd335
123 100+
Thanks..It did help me too..
Apr 14 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by John Aherne | last post: by
6 posts views Thread by Martin Bischoff | last post: by
2 posts views Thread by SiouxieQ | last post: by
2 posts views Thread by fool | last post: by
13 posts views Thread by programming | last post: by
8 posts views Thread by Horacius ReX | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.