Re: print matching lines
by davidrw (Prior) on Jul 10, 2005 at 19:20 UTC
|
You said each entity in the file is composed of 4 lines but the second one in your example only has 3 lines..
Do you have code/thoughts you've tried so far?
one method would be to read 4 lines at a time. if the 4th line matches, print the first line. Another method would be to set $/ to '# input' and then regex the resulting 'lines' (or grep all of them). | [reply] [d/l] |
|
|
sorry yes,
# input 2
xxx
yyyy
1
so with $/ set to # input it would be possible to do a pattern search and print the match?
| [reply] |
|
|
yes, that's why i suggested it .. I would (you ignored my questioned about providing code, so i'll continue just guiding with pesudo code until you have something to work from) set $/, then loop through the 'lines' (which will contain multiple \n newlines). During this loop, split on newline to get your array of 4 lines. Note the first one will be just a number (the remainder of the '# input 3' line) and the last one will be '# input ' ($/).
| [reply] [d/l] [select] |
|
|
# input 2
xxx
yyy
1
| [reply] [d/l] |
|
|
Re: print matching lines
by Forsaken (Friar) on Jul 10, 2005 at 19:38 UTC
|
Sorry, but your question doesn't make sense. Should it print # input 1 if any of the other inputs have the same string on line 3? Because in that case you might switch the whole thing around and just look for unique occurences of line 3 and print everything else. Please provide some more information on what you want exactly as well as some code you've already tried.
update to prevent the question thread going 16 levels deep:
how about something like: (untested code alert)
use strict;
use warnings;
my $FH = *insert your filehandle stuff here*;
my $linecounter = 0;
my $headerline;
while(my $line = <$FH>)
{
chomp($line); #not sure if needed...
unless(my $modulo = ($linecounter % 4))
{ $headerline = $line; }
elsif(($modulo == 3) && ($line eq 'ord'))
{ print "$headerline\n"; }
$linecounter += 1;
}
| [reply] [d/l] [select] |
|
|
if the 4ith line matches ord it should print the # input line
| [reply] |
|
|
so are you looking for a match between # input 1 and another one? or between any 2 inputs? and, another important question, are all the inputs numerically ordered like that?
| [reply] |
|
|
|
|
|
|
|
|
|
|
Re: print matching lines
by Adrade (Pilgrim) on Jul 11, 2005 at 03:13 UTC
|
while(<DATA>){$q[($.-1)%4]=$_;print $q[0] if (($.-1)%4==3)&&(m/ord/s)}
__DATA__
# input 1
xxx
yyyy
ord
# input 2
xxx
yyy
1
# input 3
xxx
yyy
ord
-- By a scallop's forelocks!
| [reply] [d/l] |
Re: print matching lines
by anonymized user 468275 (Curate) on Jul 11, 2005 at 11:51 UTC
|
...or a simple alternative way...
my @block;
while ( <$fh> ) {
if ( /^\#/ ) {
if ( $block[3] =~ /^ord\s*$/ ) {
print $block[0];
}
$#block = -1;
}
push @block, $_;
}
| [reply] [d/l] |