Skip to main content
added 199 characters in body
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

I don't quite see why you would be wanting to pass any shell variables to awk here.

tail "$(date +'%Y%m%d.txt')" | awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' 

tail extracts the last 10 lines of text from the given file by default, so -n 10 (or the deprecated -10) is not needed. The date command is used to create the filename to read from.

The awk code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Note that there is no use for shell variables here.

I don't quite see why you would be wanting to pass any shell variables to awk here.

tail "$(date +'%Y%m%d.txt')" | awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' 

The code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Note that there is no use for shell variables here.

I don't quite see why you would be wanting to pass any shell variables to awk here.

tail "$(date +'%Y%m%d.txt')" | awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' 

tail extracts the last 10 lines of text from the given file by default, so -n 10 (or the deprecated -10) is not needed. The date command is used to create the filename to read from.

The awk code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Note that there is no use for shell variables here.

deleted 96 characters in body
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

I don't quite see why you would be wanting to pass any shell variables to awk here.

tail "$(date +'%Y%m%d.txt')" | awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' file 

Running this with your data in file produces

lawrence 2 mark 2 mike 3 

The code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Would you want to sort this by decreasing count, then pipe it through sort -k 2,2nrNote that there is no use for shell variables here.

I don't quite see why you would be wanting to pass any shell variables to awk here.

awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' file 

Running this with your data in file produces

lawrence 2 mark 2 mike 3 

The code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Would you want to sort this by decreasing count, then pipe it through sort -k 2,2nr.

I don't quite see why you would be wanting to pass any shell variables to awk here.

tail "$(date +'%Y%m%d.txt')" | awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' 

The code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Note that there is no use for shell variables here.

Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

I don't quite see why you would be wanting to pass any shell variables to awk here.

awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' file 

Running this with your data in file produces

lawrence 2 mark 2 mike 3 

The code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Would you want to sort this by decreasing count, then pipe it through sort -k 2,2nr.