Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^3: find common data in multiple files

by thanos1983 (Vicar)
on Dec 29, 2017 at 14:12 UTC ( #1206415=note: print w/replies, xml ) Need Help??

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

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 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!

Replies are listed 'Best First'.
Re^4: find common data in multiple files
by mao9856 (Sexton) on Dec 31, 2017 at 06:18 UTC

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

    $ perl *.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 *.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.)


      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

      Sample of code:

      $ perl *.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!

        Hello Thank you for your time. I am using Linux Ubantu 16.04. Here is sample of dir

        $ ls -la total 272 drwxrwxr-x 2 meetal meetal 4096 Jan 3 12:06 . drwxrwxr-x 3 meetal meetal 4096 Jan 3 11:54 .. -rw-rw-r-- 1 meetal meetal 20188 Jan 3 11:54 4_os_10.txt -rw-rw-r-- 1 meetal meetal 14225 Jan 3 11:54 4_os_11.txt -rw-rw-r-- 1 meetal meetal 20788 Jan 3 11:54 4_os_12.txt -rw-rw-r-- 1 meetal meetal 3217 Jan 3 11:54 4_os_13.txt -rw-rw-r-- 1 meetal meetal 13845 Jan 3 11:54 4_os_14.txt -rw-rw-r-- 1 meetal meetal 1117 Jan 3 11:54 4_os_15.txt -rw-rw-r-- 1 meetal meetal 1929 Jan 3 11:54 4_os_16.txt -rw-rw-r-- 1 meetal meetal 15223 Jan 3 11:54 4_os_17.txt -rw-rw-r-- 1 meetal meetal 2020 Jan 3 11:54 4_os_18.txt -rw-rw-r-- 1 meetal meetal 14866 Jan 3 11:54 4_os_19.txt -rw-rw-r-- 1 meetal meetal 3068 Jan 3 11:54 4_os_1.txt -rw-rw-r-- 1 meetal meetal 16040 Jan 3 11:54 4_os_20.txt -rw-rw-r-- 1 meetal meetal 10557 Jan 3 11:54 4_os_2.txt -rw-rw-r-- 1 meetal meetal 10653 Jan 3 11:54 4_os_3.txt -rw-rw-r-- 1 meetal meetal 6558 Jan 3 11:54 4_os_4.txt -rw-rw-r-- 1 meetal meetal 18823 Jan 3 11:54 4_os_5.txt -rw-rw-r-- 1 meetal meetal 5934 Jan 3 11:54 4_os_6.txt -rw-rw-r-- 1 meetal meetal 17570 Jan 3 11:54 4_os_7.txt -rw-rw-r-- 1 meetal meetal 7785 Jan 3 11:54 4_os_8.txt -rw-rw-r-- 1 meetal meetal 13825 Jan 3 11:54 4_os_9.txt -rw------- 1 meetal meetal 12288 Jan 3 11:54 -rw-rw-r-- 1 meetal meetal 651 Jan 3 11:54

        And output i get

        $ perl *.txt $VAR1 = [];
        Regards mao9856

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1206415]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-07-17 06:27 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (355 votes). Check out past polls.