0

File1

ab 
abcd 
ababa

File2

sieraa  .alu.wood.unitedcarrot ababa.alu.wood.unitedcarrot_159273  A-AMA_CDU  oneday  5 21/06/2020 19:37:41 35:34:35 22/09/2020 19:37:41 146294407 BOND
sieraa  alu.wood.unitedcarrot ababa.alu.wood.unitedcarrot_15923  A-AMA_CDU-001  oneday 3 17/06/2020 19:18:34 01:01:01 18/07/2020 19:18:34 13412337 BOND
sieraa  .alu.wood.unitedcarrot aca.alu.wood.unitedcarrot_15930  tata-papa twoday  3 25/06/2020 00:30:26 00:59:31 26/07/2020 00:30:26 31124932 helloworld
sieraa  .alu.wood.unitedcarrot abcd.alu.wood.unitedcarrot_159292  core-core  twoday  3 24/06/2020 00:30:06 02:44:27 25/07/2020 00:30:06 84174706 helloworld

Output required print matched string and unmatched string

Ques: I have two files one File1 - contains single alphabets and file2 contains multiple strings in single to multiple line

I want to match alphabets in contained in File1 in File2 and if they match say match and Print whole line of File2 and

if they dont match print unmatched alphabet of file1 as unmatch

perl Programmers pls help

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • What do you mean by *"print unmatched alphabet of file1 as unmatch"* ? For example the first line matches both `ab` and `ababa` but not `abcd`. What would be the output for this line? – Håkon Hægland Jun 25 '20 at 13:24
  • I mean the string that do not match the variable or value . Please also note that file 1 has 1000 records and file2 has 1 million records and using shell grep it is getting very slower even using while loop , Perl uses it efiiciently , Therefore if you /anyone has perl code for this . i will highly appreciate that – Vishwas Singh Jun 25 '20 at 15:42
  • See also [Fastest way to find lines of a file from another larger file in Bash](https://stackoverflow.com/q/42239179/2173773) – Håkon Hægland Jun 25 '20 at 16:08
  • Here is one more that could be of interest: [Perl: match against a large array](https://stackoverflow.com/q/62080845/2173773) – Håkon Hægland Jun 25 '20 at 16:20
  • Please show complete exact expected output for the example in your question. It is still not clear how the output should look – Håkon Hægland Jun 25 '20 at 16:23
  • Is file1 contains only one word or multiple words. if it is single word, you want to match as whole word or partial match. Please provide clear input and output sample. – Rajeshkanna Purushothaman Jun 26 '20 at 12:59
  • the output should look line output ------------------------------- matched values 00:30:26 31124932 helloworld sieraa .alu.wood.unitedcarrot abcd.alu.wood.unitedcarrot_159292 core-core twoday 3 24/06/2020 00:30:06 02:44:27 25/07/2020 00:30:06 84174706 helloworld unmatched strings ab ababa pls note file1 has single string million records with only 1 column e.g ab abocd ama hahuhi hwang mike schul etc etc ..... and file 2 has million lines e.g sieraa .alu.wood.unitedcarrot ababa.alu.wood.unitedcarrot_159273 A-AMA_CDU oneday 5 21/06/2020 19:37:41 35:34:35 22 so on ... – Vishwas Singh Jun 26 '20 at 14:56

2 Answers2

0

Try this one-liner:

cat File1 | while read i; do echo -e "\nPattern=$i"; grep "$i" File2; [ $? -ne 0 ] && echo "No match"; done
LeadingEdger
  • 604
  • 4
  • 7
  • Thnx Leading edger ..Shell loop is working very slow when both files have millsion records . This is the problem with Shell loop – Vishwas Singh Jul 03 '20 at 05:33
0

Here's the equivalent in Perl:

perl -e 'BEGIN
{$FILEONE=shift;
open (FILEONE, "$FILEONE") or die $!;
@ONE = <FILEONE>;

$FILE2=shift;
open (FILE2, "$FILE2") or die $!;
@TWO = <FILE2>;

print "Done reading files into arrays\n";
}

foreach $i (@ONE) {
  print "\nPattern=$i";
  print ( grep ( /$i/x, @TWO));};
'  File1 File2

NOTE: This assumes that computer memory is not an issue :-)

LeadingEdger
  • 604
  • 4
  • 7
  • There are several lines those are matching ing grep if two lines are sufficent . could you share the logic also those pattern that do not match shall i apply else or make another loop....thnx a lot for great consulting provided – Vishwas Singh Jul 04 '20 at 03:55