Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: find common data in multiple files

by mao9856 (Sexton)
on Dec 29, 2017 at 10:38 UTC ( #1206405=note: print w/replies, xml ) Need Help??


in reply to Re: find common data in multiple files
in thread find common data in multiple files

Thank you for help. I tried to write this code based on my understanding. Please excuse me. I am very beginner of perl. My data contain unique ids (ID157) and name (TSR11) separated by tab. i want to look for both ids and name (ID157 TSR11) if they are present in all 25 files. If ID157 TSR11 is present in all 25 files, it should be printed in the output. This i want to print only those IDs and name that are present in all 25 files. and id and name should print together separated by tab as: ID157 TSR11. I am less familiar with using perl modules, but i am trying my best.

  • Comment on Re^2: find common data in multiple files

Replies are listed 'Best First'.
Re^3: find common data in multiple files
by thanos1983 (Vicar) on Dec 29, 2017 at 14:12 UTC

    Hello again mao9856,

    Not knowing it is not a problem, nobody started coding and knew everything. This forum is open and free for people to learn and contribute. You provide us a bit of a non working code. You either got it from someone or you modified it to the point it was not working. It is good for you and all of us to practice and try to provide a working sample of code that shows what you have tried and where you got stuck. For us to provide you a solution it is really easy but it would not mean that it resolves your problem since you will not learn anything out of it.

    Having said that here is a sample of code that it does what you want.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use List::MoreUtils 'frequency'; my (@lines); my $numberOfFiles = scalar @ARGV; while (<>) { next if /^\s*$/; # skip empty lines (remove if not needed) chomp; push @lines, $_; } continue { close ARGV if eof; # Not eof()! } my @frequencyLines = frequency @lines; my %frequencyHash = @frequencyLines; my @unwanted; foreach my $key (keys %frequencyHash) { if ($frequencyHash{$key} != $numberOfFiles) { push @unwanted, $key; # push any related keys onto @unwanted } } delete @frequencyHash{@unwanted}; my @matches = keys %frequencyHash; print Dumper \@matches; __END__ $ perl test.pl File1.txt File2.txt File3.txt $VAR1 = [ 'ID122 EFG87', 'ID121 ABC14', 'ID157 TSR11' ];

    I used as input files the first 3 files (File 1 -3) as input DATA that you provided us.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Hi BR, I tried this code using it as follow:

      $ perl test.pl *.txt

      And it is giving output:

      $VAR1 = [];

      Please help me understand how should it work. Thank you in advance

        I tried this code using it as follow:

        $ perl test.pl *.txt

        And it is giving output:

        $VAR1 = [];

        Are you running the script in the directory containing the *.txt files?

        What operating system are you using? If you are on Windows, there are some minor differences to Unix that prevent *.txt from being expanded to a list of files. In that case, add the following line to the perl script, right after "use warnings;":

        use Win32::Autoglob;

        (See Re^3: Perl Rename for details why this is needed.)

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Hello mao9856

        I think your question has already being answered by fellow Monk afoken. Fo me in UnixOS it looks that it is working fine, see sample of code bellow. For any reason if the script is not working on your OS update us and provide more information so we can assist you.

        Sample of dir:

        $ ls -la total 36 drwxr-xr-x 2 tinyos tinyos 4096 Jan 2 10:42 . drwxr-xr-x 5 tinyos tinyos 4096 Jan 2 10:41 .. -rw-r--r-- 1 tinyos tinyos 97 Jan 2 10:41 File1.txt -rw-r--r-- 1 tinyos tinyos 85 Jan 2 10:41 File2.txt -rw-r--r-- 1 tinyos tinyos 74 Jan 2 10:41 File3.txt -rw-r--r-- 1 tinyos tinyos 651 Jan 2 10:42 test.pl

        Sample of code:

        $ perl test.pl *.txt $VAR1 = [ 'ID157 TSR11', 'ID121 ABC14', 'ID122 EFG87' ];

        Hope this helps, Best Regards (BR).

        Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1206405]
help
Chatterbox?
[Discipulus]: as a greed Agrid I agree.. coffee!

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-05-21 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?