file1.txt#! /usr/bin/perl -w use strict; my @arr=('perl','monks','website'); our @files=('file1.txt','file2.txt','file3.txt'); open(LOG,">","output.txt"); my $file; foreach $file(@files) { open(FILE,"<",$file); my @lines=<FILE>; close(FILE); my $count=0; my $line; foreach $line(@lines) { $count++; if($line =~ /.*$arr[0]/) { print LOG "$arr[0] found in $file at line number $count\n"; } if($line =~ /.*$arr[1]/) { print LOG "$arr[1] found in $file at line number $count\n"; } if($line =~ /.*$arr[2]/) { print LOG "$arr[2] found in $file at line number $count\n"; } } } close(LOG);
file2.txtansh batra perl postgres apache monks
file3.txtansh batra is in perl monks apache postgres
output.txtwebsite monks perl website ansh ansh website
thanks!!!perl found in file1.txt at line number 3 monks found in file1.txt at line number 6 perl found in file2.txt at line number 1 monks found in file2.txt at line number 1 website found in file3.txt at line number 1 perl found in file3.txt at line number 2 monks found in file3.txt at line number 2 website found in file3.txt at line number 2 website found in file3.txt at line number 5
In reply to Re: Searching through a word file using an array element
by ansh batra
in thread Searching through a word file using an array element
by coldwish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |