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?
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.
Comments