Adding file name to the counted data

Viewed 85

Suppose I have files similar to the following.

file 1

1,144931087,144931087,T,C  
16,89017167,89017167,C,G  
17,7330235,7330235,G,T  
17,10222478,10222478,C,T  

file 2

1,144931087,144931087,T,C
16,89017167,89017167,C,G
17,10222478,10222478,C,T

file 3

17,10222478,10222478,C,T  

I would like to find how many times duplicated values are present in each file, So ideally, the output would be like:

Output

2 1,144931087,144931087,T,C  
2 16,89017167,89017167,C,G  
3 17,10222478,10222478,C,T  
1 17,7330235,7330235,G,T 

I used the following command for counting the duplicates value.

sort Test1.csv Test2.csv Test3.csv | uniq --count

Now I wish to add the file name for the counted output. My desired output should look like this:

Test1 Test2 2 1,144931087,144931087,T,C  
Test1 Test2 2 16,89017167,89017167,C,G  
Test1 Test2 Test 3 3 17,10222478,10222478,C,T  
Test1 1 17,7330235,7330235,G,T  

Can anyone help me to get the desired output or can anyone suggest me a better way to get my desired output?

4 Answers

Using awk. Sorry about my clever file naming scheme:

$ awk '{
    a[$0]++                   # count hits
    b[$0]=b[$0] FILENAME " "  # store filenames
}
END {
    for(i in a)               
        print b[i] a[i],i     # output them
}' foo bar baz
foo bar 2 1,144931087,144931087,T,C
foo bar 2 16,89017167,89017167,C,G
foo bar baz 3 17,10222478,10222478,C,T
foo 1 17,7330235,7330235,G,T

UPDATED per comments:

$ awk 'BEGIN {
    FS=OFS=","
} 
{
    a[$1 OFS $2 OFS $3 OFS $4]++ 
    b[$1 OFS $2 OFS $3 OFS $4]=b[$1 OFS $2 OFS $3 OFS $4] FILENAME "|"
    c[$1 OFS $2 OFS $3 OFS $4]=$0                      # keep the last record with 
}                                                      # specific key combination 
END { 
    for(i in a) 
        print b[i] "," a[i],c[i]  
}' foo  bar baz
foo|bar|,2,16,89017167,89017167,C
foo|,1,17,7330235,7330235,G
foo|bar|,2,1,144931087,144931087,T
foo|bar|baz|,3,17,10222478,10222478,C

Input:

more Test*.csv
::::::::::::::
Test1.csv
::::::::::::::
1,144931087,144931087,T,C
16,89017167,89017167,C,G
17,7330235,7330235,G,T
17,10222478,10222478,C,T
::::::::::::::
Test2.csv
::::::::::::::
1,144931087,144931087,T,C
16,89017167,89017167,C,G
17,10222478,10222478,C,T
::::::::::::::
Test3.csv
::::::::::::::
17,10222478,10222478,C,T

Command:

awk '{tmp[$0]++;if(length(tmp2[$0])==0){tmp2[$0]=FILENAME;next}tmp2[$0]=tmp2[$0] OFS FILENAME}END{for(elem in tmp){print tmp2[elem] OFS tmp[elem] OFS elem}}' Test*.csv

Output:

Test1.csv Test2.csv 2 1,144931087,144931087,T,C
Test1.csv Test2.csv 2 16,89017167,89017167,C,G
Test1.csv Test2.csv Test3.csv 3 17,10222478,10222478,C,T
Test1.csv 1 17,7330235,7330235,G,T

Explanations:

  # gawk profile, created Mon Dec 17 14:46:47 2018

  # Rule(s)

   {
           tmp[$0]++ #associative array to count the occurrences freq
           if (length(tmp2[$0]) == 0) {  #when you add the first occurrence filename you do not need to add a space
                   tmp2[$0] = FILENAME
                   next
            }
           #append to variable with a space
           tmp2[$0] = tmp2[$0] OFS FILENAME
    }

    # END rule(s)

    END {
           # loop on each element of the associative arrays and print them
           for (elem in tmp) {
                   print tmp2[elem] OFS tmp[elem] OFS elem
            }
    }

The if...next... can be replaced by (length(tmp2[$0]) == 0 ? tmp2[$0] = FILENAME : tmp2[$0] = tmp2[$0] OFS FILENAME) to simplify the awk script into:

  {
       tmp[$0]++
       (length(tmp2[$0]) == 0 ? tmp2[$0] = FILENAME : tmp2[$0] = tmp2[$0] OFS FILENAME)
  }

  END {
         for (elem in tmp) {
              print tmp2[elem] OFS tmp[elem] OFS elem
         }
  }

Could you please try following and this should give you output into Input_file's line's Input occurrence. I have used gsub(/[[:space:]]+$/,"") since your Input_file(s) have spaces in last of lines so removing them here, you could remove it in case it is NOT the case.

awk '
{
  gsub(/[[:space:]]+$/,"")
}
!a[$0]++{
  b[++count]=$0
}
{
  c[$0]++
  d[$0]=d[$0]?d[$0] OFS FILENAME:FILENAME
}
END{
  for(i=1;i<=count;i++){
    print d[b[i]]"|"c[b[i]],b[i]
  }
}'  test1 test2 test3

Output will be as follows.

test1 test2|2 1,144931087,144931087,T,C
test1 test2|2 16,89017167,89017167,C,G
test1|1 17,7330235,7330235,G,T
test1 test2 test3|3 17,10222478,10222478,C,T

One more answer using Perl.

> cat file1m.csv
1,144931087,144931087,T,C
16,89017167,89017167,C,G
17,7330235,7330235,G,T
17,10222478,10222478,C,T
> cat file2m.csv 
1,144931087,144931087,T,C
16,89017167,89017167,C,G
17,10222478,10222478,C,T
> cat file3m.csv
17,10222478,10222478,C,T
> cat uniq_perl.ksh
perl -lne ' 
@t=@{ $kvf{$_} };
if( not $ARGV ~~ @t ) { push(@t,$ARGV); $kvf{$_}=[ @t ] ;  }
close(ARGV) if eof; 
END { for(keys %kvf) { @x=@{$kvf{$_}};  print join(" ",@x)." ".scalar(@x)." ".$_  } }   
' file*m*csv 
> ./uniq_perl.ksh
file1m.csv file2m.csv file3m.csv 3 17,10222478,10222478,C,T
file1m.csv 1 17,7330235,7330235,G,T
file1m.csv file2m.csv 2 1,144931087,144931087,T,C
file1m.csv file2m.csv 2 16,89017167,89017167,C,G
> 
Related