Sorting help with bash

Using Mac OS 10.15.x Catalina

Consider the following CSV:

Name Error Count Severity Info
Server1 not found 1025 MAJOR some info here
Server4 some error 1 MAJOR some info here
Server 2 another error 587 MINOR more info
Server3 not found 15 MAJOR more info

I have some code to sort on the Severity, however, I can't get the numerical sort on the count to work correctly. I try to sort it numerically first, and then by severity, but the numerical portion doesn't seem to work. Here is what I have:

sort -k3,1 "$1";( grep -i "name" "$1"; grep "CRITICAL" "$1"; grep "MAJOR" "$1"; grep "HIGH" "$1"; grep "MEDIUM" "$1"; grep "MINOR" "$1"; grep "LOW" "$1" | sort ) > $OUT

RESULTS: It sorts on severity just fine, but doesn't sort on Count. Let me know if that doesn't make sense.

Desired outcome (notice the Count is from greatest to least within each sub-set of Severity)

Name Error Count Severity Info
Server1 not found 1025 MAJOR some info here
Server3 not found 15 MAJOR more info
Server4 some error 1 MAJOR some info here
Server 2 another error 587 MINOR more info

Hi
I am not familiar with Mac OS but you can try the following code or create your own based on it.
I think the principle is clear

for i in Name CRITICAL MAJOR HIGH MEDIUM MINOR LOW; do
        grep "$i" file | sort -k4,4nr
done

--- Post updated at 16:03 ---

head -1 file
for i in CRITICAL MAJOR HIGH MEDIUM MINOR LOW; do
        grep "$i" file | sort -k4nr
done

Thank you for the suggestion. It doesn't work 100%. I cut out sensitive fields, but this gives you an idea of the results:

CODE (Note the first filed is to just grab the title/headers of the columns):

for i in RecommendedActions CRITICAL MAJOR HIGH MEDIUM MINOR LOW; do
  grep "$i" "$1" | sort -k3,3nr >> $OUT
done

RESULT:

ErrorCount	Severity
372	MAJOR
578	MAJOR
31249	MAJOR
8306	MAJOR
16	HIGH
17	HIGH
3236	MINOR
367	MINOR
2	MINOR
19	MINOR
85721	LOW
564	LOW
729	LOW
128424	LOW
220	LOW
25516	LOW
5	LOW
343080	LOW
3705	LOW
158	LOW
92	LOW

Turn on the option sort -k3,3nr --debug and see where the error is

--- Post updated at 19:00 ---

head -5 file | sort -k3,3nr --debug

With a help file in $SEV defining severity order in the form

1 CRITICAL 
3 HIGH 
6 LOW
2 MAJOR 
4 MEDIUM 
5 MINOR

try also

grep -Eo "[0-9]* *(CRITICAL|MAJOR|HIGH|MEDIUM|MINOR|LOW)" $1 | sort -k2 -k1.1nr | join -j2 - $SEV | sort -k3 -k2nr | cut -f1,2 -d" "

I didn't know how to interpret the debug output. I tried throwing in -t "," into my sort and now it is working. Don't know why I need to do that, as I thought it was the default, but it seems to work with that in there..... (don't have commas anywhere in this doc other than as field separators)

sort -t "," -k3,3nr "$1" > $OUT
for i in RecommendedActions CRITICAL MAJOR HIGH MEDIUM MINOR LOW; do
  grep "$i" "$OUT"  >> $OUT2
done

I appreciate all of the help. Thank you

Ok, The right time to learn how to use the '--debug' option?

sort -k3nr --debug <<<"Server1 not found 1025 MAJOR some info here"
sort: text ordering performed using simple byte comparison
sort: key 1 is numeric and spans multiple fields
Server1 not found 1025 MAJOR some info here
            ^ no match for key
___________________________________________

mismatch!

sort -k4nr --debug <<<"Server1 not found 1025 MAJOR some info here"
sort: text ordering performed using simple byte comparison
sort: key 1 is numeric and spans multiple fields
Server1 not found 1025 MAJOR some info here
                  ____
___________________________________________

You see, a little dash appeared under the number?
This first key sorting rule worked. But there are more clues

sort -k4,4nr --debug <<<"Server1 not found 1025 MAJOR some info here"
sort: text ordering performed using simple byte comparison
Server1 not found 1025 MAJOR some info here
                  ____
___________________________________________

That's ok

--- Post updated at 22:18 ---
Also
Try it like this

head -1 file
for i in CRITICAL MAJOR HIGH MEDIUM MINOR LOW; do
        grep "$i" file | sort -k4
done

Sorry, bad advice

1 Like

If the 'Error' field has an undefined number of spaces, you can set the necessary delimiters.
also you can separate and display the title at once

sed -nr '1p;s/\s([0-9]+)\s+([[:upper:]]+)\s/_\1_\2_/w file2' file1
for i in CRITICAL MAJOR HIGH MEDIUM MINOR LOW; do
        grep "$i" file2 | sort -t_ -k2,2nr
done | tr _ ' '

if necessary remove delimiters '_' using the trailing 'tr' command