Re: Reading a file from any arbitrary place in the file.
by chibiryuu (Beadle) on Nov 21, 2005 at 19:51 UTC
|
1 .. /^This is the temp line\.$/ or print while <>;Use of the range operator in scalar context, Yay! If you want to include "This is the temp line", you can do this: /^This is the temp line\.$/ .. 0 and print while <>; | [reply] [d/l] [select] |
Re: Reading a file from any arbitrary place in the file.
by diotalevi (Canon) on Nov 21, 2005 at 19:38 UTC
|
{
# Temporarily set $/.
local $/ = "This is the temp line.\n";
<>;
}
while ( <> ) {
print;
}
Thanks Roy Johnson for the tip on the spelling error. | [reply] [d/l] |
Re: Reading a file from any arbitrary place in the file.
by davidrw (Prior) on Nov 21, 2005 at 19:36 UTC
|
Basic approach is to read through the while file, and set the flag once you hit your marker, and print if the flag is set.
perl -ne 'if($ok){print;}else{$ok=/^This is the temp line\.$/}' foo.tx
+t
Another approach, if the file is small enough, is to slurp into memory and split it, using your marker as the delimiter:
perl -n0777e 'print ((split(/^This is the temp line.\n/m, $_, 2))[-1])
+' foo.txt
| [reply] [d/l] [select] |
Re: Reading a file from any arbitrary place in the file.
by Anonymous Monk on Nov 21, 2005 at 19:23 UTC
|
Do you mean to print all of the lines after "This is the temp line."? If so, this should do the trick:
$print = 0;
while ( <DATA> ) {
if ( $print ) {
print;
}
if ( /^This is a temp line\./ ) {
$print = 1;
}
}
| [reply] [d/l] |
|
|
Sorry, typed too fast; the regex should be "This is the temp line". I'm sure you get the idea, though.
| [reply] |
Re: Reading a file from any arbitrary place in the file.
by Anonymous Monk on Nov 22, 2005 at 00:41 UTC
|
If your description is the entire problem, I would suggest using sed instead of Perl because it has syntax for dealing with exactly this kind of operation.
sed -n "/This is the temp line/,$"p infile > outfile
I'm not hating on Perl by any means, I just use the simplest tool for the job. Of course if you have to do anything more than a regex or two with the text before generating the output, stick with Perl. | [reply] [d/l] |
|
|
Or, if you do not want to include the temp line:
sed -n "1,/This is the temp line/"!p infile > outfile
| [reply] [d/l] |
Re: Reading a file from any arbitrary place in the file.
by TedPride (Priest) on Nov 21, 2005 at 23:32 UTC
|
use strict;
use warnings;
my ($line, $length, $in, $out, $inh, $outh);
$line = 'This is the temp line.';
$in = 'in.dat';
$out = 'out.dat';
$length = length($line) + 1;
open ($inh, $in);
open ($outh, ">$out");
while (<$inh>) { last if $length == length($_) && index($_, $line) !=
+-1; }
while (<$inh>) { print $outh $_; }
close($outh);
close($inh);
| [reply] [d/l] |
Re: Reading a file from any arbitrary place in the file.
by bfilipow (Monk) on Nov 22, 2005 at 09:26 UTC
|
~$ cat monks001.txt
hdhdklsklfhklfhfl
hklhifdhfeihfiohfhf
hlhfdlhdfdhflfhhfhf
oioooooooooooooo
This is the temp line.
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaa
sssssssssssssssssssss
~$ perl -ne 'print unless 1 .. /temp/' monks001.txt
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaa
sssssssssssssssssssss
~$
| [reply] [d/l] |
Re: Reading a file from any arbitrary place in the file.
by Knom (Beadle) on Nov 22, 2005 at 01:45 UTC
|
use strict;
use warnings;
my ($trigger, $fh_in, $fh_out);
my $split_text = 'This is the temp line.';
my $in = 'input_file.dat';
my $out = 'output_file.dat';
open ($fh_in, $in) or die "ERROR: opening file $in: $@\n";
open ($fh_out, ">$out") or die "ERROR: opening file $out: $@\n";
undef $trigger;
while (<$fh_in>) {
unless($trigger || /$split_text/) {
next;
} # END unless($trigger || /$split_text/)
if(/$split_text/) {
$trigger=1;
next; # Else delete or comment out this line if you want the
+ split_line printed.
} # END if(/$split_text/)
print $fh_out $_;
} # END while (<$fh_in>)
close($fh_in);
close($fh_out);
| [reply] [d/l] |
Re: Reading a file from any arbitrary place in the file.
by Anonymous Monk on Nov 22, 2005 at 09:28 UTC
|
#!/usr/bin/perl
open(DATA,"mahi");
$a="";
$a=$a.$_ while($_= <DATA>);
$a =~ /This is the temp line\./g;
print $`;
Janitored by holli. Added code tags | [reply] [d/l] |