Elegant way of counting how many times patterns from a file occur in another file

pnom

Imagine that we have two for example files. The first file is filled with unique names of employees created by combining the first two characters of the first name and the last 2 characters of the last name. Example : Peter Smith - Peht

First file contains :

Peht
Mawo
Stso
Makr
Bavo

The second file contains recordings about them when they logged into the system. ( Obviously there are also employees that are not listed in file1. )

The second file:

Mawo 21.4.2016 17:49
Peht 21.4.2016 17:58
Mawo 22.4.2016 7:58
Wato 22.4.2016 7:59
Stso 22.4.2016 8:02
Bavo 22.4.2016 8:15
Bane 22.4.2016 9:01
Bavo 23.4.2016 9:12
Mawo 23.4.2016 9:24
Dalo 23.4.2016 9:54
Peht 23.4.2016 9:58
Grma 24.4.2016 10:00

I need to find out how many times employes from file1 connected to the system ( file2 ).

What is the best way of doing that? The only solution which came to my mind is to make some 2 loops and for each name from file1 loop the whole file2 then grep names, ask if the names match if yes then count++. Could anyone give me some elegant solution for this problem using for example awk if it's possible?

Jeff Schaller

Something simple like:

mapfile -t names < file1
for name in "${names[@]}"
do
  echo "${name}" $(grep -c "^$name " file2)
done

Will provide output like:

Peht 2
Mawo 3
Stso 1
Makr 0
Bavo 2

The grep string says to anchor the username at the beginning (^) of the line, and enforce a trailing space after the line.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count how many times numbers from one array occur in another

From Dev

Searching a txt file for a string and then counting how many times it appears

From Dev

How many times a file was requested from a server?

From Dev

Printing only the duplicates from an int array and how many times they occur

From Dev

Search for text patterns from a file in another file

From Dev

Counting how many times one value is in the same series of another df

From Dev

How to smooth numbers from a file as many times as wanted in Python 2.7?

From Dev

How to sync file times from one directory tree to another

From Dev

Modify filenames with patterns from another file

From Dev

match at least two patterns from another file

From Dev

How many times a TypeScript file compiles?

From Dev

Elegant way to .seek on an open file

From Dev

An elegant, readable way to read Butcher tableau from a file

From Dev

c++ String from file to vector - more elegant way

From Dev

Is there a way to know how many times a static file is served or it has been served or not at all in Node.js?

From Dev

Removes values in a file that match patterns from another file

From Dev

Replacing strings in file, using patterns from another file

From Dev

counting how many weeks of the year date ranges cover/occur in

From Dev

Counting how many times variable has changed

From Dev

Counting how many times a loop runs

From Dev

Counting how many times each vowel appears

From Dev

Counting how many times an image appears on screen

From Java

How to see how many times words from string array are present in a text file

From Dev

How can I read in values from a text file and calculate how many times a value repeats and then find the average?

From Dev

Call a PHP file from another one multiple times (+ file reading)

From Dev

Preffered way of counting lines, characters and words from a file as a whole in Python

From Dev

Python subtract json objects from one json file if they occur in another json file

From Dev

Is there a more elegant way of counting combinations of booleans from 2 arrays?

From Java

How to make "grep" read patterns from a file?

Related Related

  1. 1

    Count how many times numbers from one array occur in another

  2. 2

    Searching a txt file for a string and then counting how many times it appears

  3. 3

    How many times a file was requested from a server?

  4. 4

    Printing only the duplicates from an int array and how many times they occur

  5. 5

    Search for text patterns from a file in another file

  6. 6

    Counting how many times one value is in the same series of another df

  7. 7

    How to smooth numbers from a file as many times as wanted in Python 2.7?

  8. 8

    How to sync file times from one directory tree to another

  9. 9

    Modify filenames with patterns from another file

  10. 10

    match at least two patterns from another file

  11. 11

    How many times a TypeScript file compiles?

  12. 12

    Elegant way to .seek on an open file

  13. 13

    An elegant, readable way to read Butcher tableau from a file

  14. 14

    c++ String from file to vector - more elegant way

  15. 15

    Is there a way to know how many times a static file is served or it has been served or not at all in Node.js?

  16. 16

    Removes values in a file that match patterns from another file

  17. 17

    Replacing strings in file, using patterns from another file

  18. 18

    counting how many weeks of the year date ranges cover/occur in

  19. 19

    Counting how many times variable has changed

  20. 20

    Counting how many times a loop runs

  21. 21

    Counting how many times each vowel appears

  22. 22

    Counting how many times an image appears on screen

  23. 23

    How to see how many times words from string array are present in a text file

  24. 24

    How can I read in values from a text file and calculate how many times a value repeats and then find the average?

  25. 25

    Call a PHP file from another one multiple times (+ file reading)

  26. 26

    Preffered way of counting lines, characters and words from a file as a whole in Python

  27. 27

    Python subtract json objects from one json file if they occur in another json file

  28. 28

    Is there a more elegant way of counting combinations of booleans from 2 arrays?

  29. 29

    How to make "grep" read patterns from a file?

HotTag

Archive