Thread Suche in bestimmten Zeilen nach bestimmtem Wort (28 answers)
Opened by kabazza at 2007-01-04 19:25

bloonix
 2007-01-08 16:50
#72881 #72881
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Falls du weisst, in welcher Zeile sich der String befindet, dann kannst du
auch direkt dorthin springen, vorausgesetzt, jede Zeile hat die gleiche
länge.

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use strict;
use warnings;

sub create_file ($$) {
  my ($lines, $file) = @_;
  open my $fh, '>', $file or die $!;
  for (1..$lines) {
     print $fh sprintf("%0".length($lines)."d", $_), " wort1 wort2 wort3\n";
  }
  close $fh;
}

sub search_in_file ($$$) {
  my ($line, $file, $search) = @_;
  open my $fh, '<', $file or die $!;
  my $first_line = <$fh>;
  my $length = length($first_line);
  seek($fh, $length*(--$line), 0);
  my $this_line = <$fh>;
  return 1 if $this_line =~ /$search/;
  return undef;
}

my $lines  = 100000;
my $search = 'wort1';
my $line   = 50000;
my $file   = './test.txt';

create_file($lines, $file);

if (search_in_file($line, $file, $search)) {
  print "HIT\n";
} else {
  print "NO HIT\n";
}
\n\n

<!--EDIT|opi|1168267865-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread Suche in bestimmten Zeilen nach bestimmtem Wort