Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^5: find common data in multiple files

by thanos1983 (Vicar)
on Jan 02, 2018 at 09:49 UTC ( #1206531=note: print w/replies, xml ) Need Help??


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

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!

Replies are listed 'Best First'.
Re^6: find common data in multiple files
by mao9856 (Sexton) on Jan 03, 2018 at 06:49 UTC

    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 .inna.pl.swp -rw-rw-r-- 1 meetal meetal 651 Jan 3 11:54 test.pl

    And output i get

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

      Hello again mao9856,

      Can you cat -A 4_os_10.txt, I would like to see all the characters format of your file. It looks it is not matching the sample of DATA that you provide us. Also try to use as @ARGV a few files, for example 4_os_10.txt and 4_os_11.txt if you know that they contain even a single line in common.

      Also there is a great answer Re: find common data in multiple files from fellow Monk BillKSmith that is using the module setop and it is simplified a lot. Sample of code on how to run the script Re^2: find common data in multiple files.

      Give it a try and let us know for both cases, BR.

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

        Hi BR

        Thank you for your time and patience. As you suggested i tried to match two files (4_os_10.txt and 4_os_11.txt) and I got desired output that is common data with your code and also with the code provided by BillKSmith. I tried it for more that 3 files .txt pair and found that maybe there isn't anything common among all 25 files.

        I found that matched data exist only in 18 files out of 25 when i used following code. Please correct me if i get it wrong. It prints matched first column with common ID (say ID121) among all 25 files and then print second name only if it exists in file. Thus output I get files column all IDs and second column of each of 25 .txt files.

        #!/usr/bin/env perl use strict; use warnings; my %data; while (<>) { my ( $key, $value ) = split; push( @{ $data{$key} }, $value ); } foreach my $key ( sort keys %data ) { if ( @{ $data{$key} } >= @ARGV ) { print join( "\t", $key, @{ $data{$key} } ), "\n"; } } $ code.pl *.txt

        Please suggest me if this code can give me desired output. Regards mao9856

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-04-20 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?