1

I am trying to grep a series of patterns that are, one per line, in a text file (list.txt), to find how many matches there are of each pattern in the target file (file1). The target file looks like this:

$ cat file1

2346 TGCA
2346 TGCA
7721 GTAC
7721 GTAC
7721 CTAC

And I need counts of each numerical pattern (2346 and 2271).

I have this script that works if you provide a list of the patterns in quotes:

 $ for p in '7721' '2346'; do   printf '%s = ' "$p"; grep -c "$p" file1; done

 7721 = 3
 2346 = 2

What I would like to do is search for all patterns in list.txt:

$ cat list.txt

7721
2346
6555
25425
22
125
....
19222

How can I convert my script above to look in the list.txt and search for each pattern, and return the same output as above (pattern = count) e.g.:

2346 = 2
7721 = 3
....
19222 = 6
LP_640
  • 579
  • 1
  • 5
  • 17

1 Answers1

2

try this awk oneliner:

 awk 'NR==FNR{p[$0];next}$1 in p{p[$1]++}END{for(x in p)print x, p[x]}' list.txt file
Kent
  • 189,393
  • 32
  • 233
  • 301