I'm looking to find out any farmer who produces bananas and his address from a database.My data looks something like this:- farmer1 address1 apple1,banana-green,orange-5  - farmer2 address2 orange-unriped6,apple-red,banana-canarvon,peach-sweet - farmer3 address3 peach-blacklisted,orange-ok,lime-unriped - farmer4 address4 banana-humungous,orange-meh,watermelon-amazing,vegetables-freshI've tried grep, cut and awk but I'm unable to print out just the key details into a text file which I hope to look something like:- farmer1 address1 banana-green- farmer2 address2 banana-canarvon- farmer4 address4 banana-humongousCan anyone help please?So after using Cas' script (thanks Cas!), i'm able to extract the information i require - which is perfect! However, i have a text file containing a list of information i require and i hope to repeat this process for the whole list (about 400 items). I've tried modifying the script to cope with a list but i'm doing something wrong. It seems to function but nothing is printing. #! /usr/bin/perl -a -nopen( GENEFILE, ActinGenesENST.txt) or die $!;open( VARFILE, Actin.ENSTvars.txt) or die $!;open( OUTPUTFILE, test.txt) or die !;print Extracting Genes\n;while (<GENEFILE>) {        if (/VARFILE/) {        @produce=grep(/VARFILE/,split(/,/,$F[9])) ;        print OUTPUTFILE join(\t,@F[0 .. 8],join(,,@produce)),\n;        }}My farmer's list is in VARFILE.My fruits list is in GENEFILE.I wish to print out returned values in TEST.TXT.
how to grep specific string in random column
grep
I put your sample data into a file called farmer.txt, and ran the following perl script:#! /usr/bin/perl -a -nif (/banana/) {  @produce=grep(/banana/,split(/,/,$F[2])) ;  print join(\t,@F[0 .. 1],join(,,@produce)),\n;}which produced this output:$ ./bananas.pl farmer.txtfarmer1 address1    banana-greenfarmer2 address2    banana-canarvonfarmer4 address4    banana-humungousFor every line in the input that contains banana, it splits the third field $F[2] by commas into a list called @produce, and uses perl's grep() function to keep only elements containing the word banana.Then it prints them out in the same format as the input.Note that if a farmer produces more than one kind of banana then this script WILL display them all.Here's a version of the script that can print multiple fruits (contained in 'fruitlist.txt'):#! /usr/bin/perl use strict;my $fruitlist='fruitlist.txt';open(FRUITS,<,$fruitlist) || die couldn't open $fruitlist: $!\n;while (<FRUITS>) {    chomp ;    my $fruit = $_;    print $fruit\n---\n;    foreach my $file (@ARGV) {      open(FILE,<,$file) || die couldn't open $file: $!\n;      while(<FILE>) {        my @F=split(/\t/);        if (/$fruit/) {          my @produce=grep(/$fruit/,split(/,/,$F[2])) ;          print join(\t,@F[0 .. 1],join(,,@produce)),\n;        }      }      close(FILE);      print \n;    }};close(FRUITS);I've dropped the perl -a (awk-like) mode and made it explicitly open files and split the contents into the @F field array because it needs to re-open the input file(s) (e.g. farmer.txt) multiple times, once for each entry in fruitlist.txt.If fruitlist.txt contains two lines (banana and apple), the scriptwill produce the following output:$ ./multifruit.pl farmer.txt banana---farmer1 address1    banana-greenfarmer2 address2    banana-canarvonfarmer4 address4    banana-humungousapple---farmer1 address1    apple1farmer2 address2    apple-red