42 lines
1.1 KiB
Awk
Executable File
42 lines
1.1 KiB
Awk
Executable File
#!/usr/bin/awk -f
|
|
|
|
# https://www.grymoire.com/Unix/Awk.html
|
|
|
|
# associative arrays
|
|
|
|
# awk supports associative arrays, which are key=value pairs rather
|
|
# values indexed by sequences of numbers. you *can* use numbers to
|
|
# index your arrays, but it isn't required.
|
|
|
|
# an associative array:
|
|
# array["banana"] = 0
|
|
# array["grapes"] = 0
|
|
# array["durian"] = 0
|
|
|
|
# then you can perform an inventory count by incrementing each fruit's
|
|
# value when you see it listed in a file:
|
|
# array["banana"]++
|
|
|
|
# the following script creates an array called 'users' and with a new
|
|
# element for each username seen in the input. if a user is seen more
|
|
# than once, the value associated with that user is incremented.
|
|
|
|
# run this script with the 'w' command to get names of logged in users:
|
|
# w | ./05.awk
|
|
|
|
{
|
|
users[$1]++
|
|
}
|
|
|
|
END {
|
|
for (i in users) {
|
|
print i, users[i]
|
|
}
|
|
}
|
|
|
|
# note that there is a bug with this overly-simple script. the 'w'
|
|
# command contains two header rows that we would normally ignore.
|
|
# the bug is left here to maintain simplicity and to highlight the
|
|
# fact that you often have to address input issues like that in
|
|
# awk scripts.
|