sabas has asked for the wisdom of the Perl Monks concerning the following question:
I have two text files 1. contains 114 lines and the 2nd txt file contains ~300K lines of which has 277 columns. i stored the 114 lines to @array and would like to search if each of them is in the ~300K, what is the best approach and quickest approach to do it? I want to use 3 ARGV in the command line ARGV[0]=my file1 with 114, ARGV2 is my second file that contains ~300K and ARGV3 to output and write the line of File2 where i found. Appreciate the help please.I am about 1 month in PERL scripting..I did not use the ARG2 and ARG3 yet to minimize my debugging, feel free to rewrite my code. How can i check the presence of 114 on each line of ~300K lines so i will not repeating the reading of ~300K and pointing the FH to 0, 0 again?
use strict; use warnings; my @sn; # store the line of file1.txt into array my $i=0; my $lctr=0; my $flag=1; while (<>) { # read the arg input file "file1.txt" +that contains info push @sn, split ' '; # store each line to array print "sn[$i]=$sn[$i]\n"; $i++; } $sn[$i] = "END"; # mark the end of the array print $sn[$i]; my $wait=<STDIN>; my $filename = 'file2.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die; $i=0; while ($flag==1) { while (my $row = <$fh>) { chomp $row; print "Searching for $sn[$i]...."; if (index($row, $sn[$i]) != -1) { print $row; print "Found $sn[$i]\n"; my $wait = <STDIN>; $i++; seek FH, 0, 0; } $lctr++; if ($sn[$i] eq 'END') { $flag=0; last; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: searching in large file
by NetWallah (Canon) on Jan 06, 2018 at 06:33 UTC | |
by sabas (Acolyte) on Jan 07, 2018 at 17:39 UTC | |
by Cristoforo (Curate) on Jan 07, 2018 at 23:37 UTC | |
by NetWallah (Canon) on Jan 08, 2018 at 03:32 UTC | |
|
Re: searching in large file
by karlgoethebier (Abbot) on Jan 06, 2018 at 12:24 UTC | |
|
Re: searching in large file
by Laurent_R (Canon) on Jan 06, 2018 at 10:33 UTC | |
|
Re: searching in large file
by thanos1983 (Parson) on Jan 06, 2018 at 14:27 UTC | |
|
Re: searching in large file
by Anonymous Monk on Jan 06, 2018 at 02:52 UTC | |
by Laurent_R (Canon) on Jan 06, 2018 at 10:13 UTC |