473,241 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 4558
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, 435 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

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

Similar topics

3
by: John Aherne | last post by:
Being a bit of a newbie, I hope this question isn't too stupid. I have searched the archives and docs for any reports about files and csv messages and not found anything that mentions the problem I...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
5
by: Patrick Vanden Driessche | last post by:
Hi All, I'm currently writing an in-house Form validation framework (WinForms) which is based on 'Component'-inheriting object. So basically, I have a small hierarchy. FormValidator +--...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
2
by: SiouxieQ | last post by:
Hi there, I'm using the code below to try to delete a name from a list of names in a file. Unfortunately it doesn't quite do what I want it to. Instead of looking for the name in the...
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
4
by: blackice | last post by:
Hello All, i have a Perl Script that deleting Zones from named.conf file and here is the script #!/usr/bin/perl -w use strict; print "please enter the domain name: "; chomp (my...
8
by: Horacius ReX | last post by:
Hi, I need to write a program which reads an external text file. Each time it reads, then it needs to delete some lines, for instance from second line to 55th line. The file is really big, so...
0
by: aadsaca | last post by:
Deleting/Removing lines on Text File in VB -------------------------------------------------------------------------------- Hi there, i just want to know the syntax on how to remove line on...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.