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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Lines from a file

Status
Not open for further replies.

newtoperl

Technical User
Sep 5, 2003
5
US
The question is.

I have a file. I search it for a string. I want to delete the line with the string as well as the lines following it until a get to another set string.

Example: I am searching for "Sun". I want to delete the line with "Sun" and everything following it until I get to "System Type" again. The file will look like this..

System Type: Sun
OS: Solaris
Version: 9

System Type: Dell
OS: Windows
Version: XP
CPUs: 2

System Type: HP
OS: HPUX
Version: ?
CPUs: 6
Memory: 64GB

The kicker is the number of lines after "Sun" may vary which is why I need to delete everything until I get to the string "System Type" again.
 
This should do the trick:

$/=undef;
open (FILEIN, "c:/log.txt");
$text = <FILEIN>;
$text =~ s/System Type: Sun.+?(System Type)/$1/is;
print $text;
close (FILEIN);

$/ is the special variable for record separator in perl. The default record separator is the newline character. When a file is being read in, each line would become a separte variable. By setting $/ to undef, perl feeds the entire file into a single variable.

Now that we have the entire file in a variable, we can pattern match it across mulitple newline characters. Even though the entire file is in a single variable, the pattern match will still die when it encounters newline characters unless you specify the &quot;s&quot; switch at the end of the regex.


Cheers
 
Hi there,

Something like this maybe.

#!/local/bin/perl

$SunFound=0;
while(<>){
if($SunFound){
next unless /^System/
$SunFound=0;
}
if(/^System Type: Sun/){
$SunFound=1;
next;
}
print;
}

This will process text on the standard input of a script and print results on its standard output.

If you put the above script in a file (called doit.pl) and then call it like this.

doit.pl yourdatafile.txt

There is a FAQ in this forum which talks about how to modify files, deleting lines and stuff. I would suggest you the example above working and then have a look at the FAQ.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Here's one way to do it:

open the file for reading
read the entire file into an array
close the file
open the file for writing and loop through the array like this:
Code:
open(F, &quot;>$file&quot;) or die &quot;Can't open $file: $!\n&quot;;
$printflag = 1;
foreach $line (@contents) {
    if ($line =~ /Sun/) {
        $printflag = 0;
    } elsif ($line =~ /System Type/) {
        $printflag = 1;
    }
    print F $line if ($printflag);
}
close(F);
 
Mike,

Nice script. I like how you treat that because you don't have to read the whole file in. I'll have to remember that. BTW, you are missing a ; here:

next unless /^System/


:)
 
oops :)

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top