Sorting and Counting output in Linux shell
Sometimes we need to make some operations on commands output in Linux. Very intensive operation on output I do is sorting and counting. For the sake of an example - every time I setup a new equipment I make backup and save it in `back` directory, with the file-names like:
ls -1 back | head -n3
101-back-2017-02-21
102-back-2017-03-01
106-back-2017-02-21
Where numbers at the starting of the names of the backups are branch codes.
When I want to learn how many equipment was installed I can simply do:
ls back | wc -l
98
But this is total number of all configured equipment. what if I want to find how much equipment I setup every month?
Let's go:
- we need to list all files in `back` directory along with their creation times:
- ll back/
- now we need to select only month (6 column) and print it:
- ll back/ | awk '{print $6}'
- now we need to sort output alphabetically:
- ll back/ | awk '{print $6}' | sort
- we needed sorting because uniq command filtering matching adjacent lines and then making operations according to specified options:
- ll back/ | awk '{print $6}' | sort | uniq
- Feb
- Mar
- we'll use `-c` which makes `uniq` count all occurrences of unique values (Mar and Feb in our case) in the output:
- ll back/ | awk '{print $6}' | sort | uniq -c
- 1
- 60 Feb
- 38 Mar
- You see line which count is `1` this is because of the `ll` commands `total` first line, to avoid this you can use:
- ll back/ | grep -v total | awk '{print $6}' | sort | uniq -c
- If you need to view results per day of the month additional use 7th column (use "-" to see `-` as month and day of the month delimiter):
- Sorting using default `sort` settings
- ll back/ | grep -v total | awk '{print $6 "-" $7}' | sort | uniq -c
- 1 Feb-20
- 59 Feb-21
- 23 Mar-1
- 9 Mar-2
- 6 Mar-7
- Soring using day of the month and numeric sorting with `-` delimiter (to sort in reverse order, use `-r` option > '-k2nr' instead of `-k2n`):
- ll back/ | grep -v total | awk '{print $6 "-" $7}' | sort -t"-" -k2n | uniq -c
- 23 Mar-1
- 9 Mar-2
- 6 Mar-7
- 1 Feb-20
- 59 Feb-21
No comments:
Post a Comment