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

Grabbing contents of 2 matched lines to store in separate file

Status
Not open for further replies.

sdslrn123

Technical User
Jan 17, 2006
78
GB
Hi. I am reading through a file:

A -
B -
C -
D -
E -
F - word; word; word;
F - word; word; finalword;
G -
H -

I am trying to grab the contents in both F lines.

At the moment I've got

#!/usr/bin/perl

foreach $line (@data) {
if ($line =~ /^F/) {
$goodline = $line;
$goodline =~ s/F//g; #take away F
$goodline =~ s/^\s+//; # remove leading whitespace
$goodline =~ s/\s+$//; # remove trailing whitespace
$goodline=~s/\s//g; # remove within whitespace
@goodvalues = split(';', $goodline);

foreach (@goodvalues){
$goodterms = "$_\n";
print "$goodterms";
}
}
}

It does the job nicely and prints results to screen.
But, how do I remove the results to a separate file.
It is because they just seem to write over one another and finalword is the result.

Thanks for looking
 
How have you tried writing to the file, are you opening the file in each iteration of the loop?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
If both contents of F have 10words. Then when I write to a file it only shows the tenth word?
It seems like I am approaching this the wrong way as I do not understand how I can get the program to go through the file and treat each line differently.
Note, sometimes F can be upto 5 lines.
 
Take the last word from a file?
Code:
use File::ReadBackwards ;
$bw = File::ReadBackwards->new( 'team.htm' ) or die "can't read file' $!" ;
$line=$bw->readline;
print $line;

This actually takes the last line, but you can work with that

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hi Paul. Sorry, no not the last word.
I want to take all the words from rows F
And print them into a list so they output
first word
second word
e.t.c
 
one possibility:

Code:
my @data = (<DATA>);
open(OUT,'>results.txt') or die "$!";
foreach $line (@data) {
   chomp;
   if ($line =~ /^F/) {
      $line =~ s/^F - //g; #take away F
      $line =~ s/^\s+//; # remove leading whitespace
      $line =~ s/\s+$//; # remove trailing whitespace
      $line=~s/\s//g; # remove within whitespace
      print OUT "$_\n" for (split(/;/, $line));
   }
}
     
__DATA__
A -
B -
C -
D -
E -
F - word; word; word;
F - word; word; finalword;
G -
H -
 
actually, if you want to remove all spaces, leading, trailing, and within, you can reduce these three lines:

Code:
$line =~ s/^\s+//; # remove leading whitespace
$line =~ s/\s+$//; # remove trailing whitespace
$line =~ s/\s//g; # remove within whitespace

to just the one line:

Code:
$line =~ s/\s+//g; # remove all whitespace
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top