1

I've been trying so solve this problem but none of the things I tried actually work. I have a table in which (from a .txt file) that I need to sort by numerical order of a specific raw of the table.

The data I have is composed like this:

Name  Team Age    
David  a   20   
James  b   25   
Anthony  c   22  
Carter  b   21   
Jessica  a   24   

Basically what I need to order (from smallest to biggest) the age of a certain team, example A. So the expected output would be:

a 20    
a 24  

4 Answers 4

1
awk '$2==team{print $2,$3}' team='a' file | sort -t' ' -k2n
Sign up to request clarification or add additional context in comments.

8 Comments

if I wanted to sort the resulting data in reverse I should be able to do it adding -r to the sort command, right? because that bit doesn't work
@AndreaCRD it works with GNU sort, but yeah you can try specifying -r separately if it doesn't work for you.
@oguzismail I tried adding -r but it doesn't reverse the order
Which sort are you using? What's the output of sort --version?
@oguzismail I'm using the version you posted, just adding the -r flag. Output is: 2.3-Apple (99)
|
0

You could handle this very easily with SQL and the SUM and GROUP BY functions:

SELECT team, SUM(age) FROM table1 GROUP BY team;

+------+----------+
| team | SUM(age) |
+------+----------+
| a    |       44 |
| b    |       46 |
| c    |       22 |
+------+----------+

1 Comment

How does this answer the OP (i.e. "in a Bash script")?
0

Bash alone is clearly not the best tool for this job.

If you love Bash code like I do, you will probably still be interested in a Bash-only solution (well, almost, let's not reinvent the sorting):

#!/bin/bash

firstline=true
declare -A sumPerTeam     # declare associative array

# collect sum for each team in the associative array:
while read name team age
do
    [ "$firstline" = true ] && { firstline= ; continue ; }    # skip header
    sumPerTeam[${team}]=$(( "${sumPerTeam[${team}]}" + age ))
done < input.txt

# output each key/value pair of the array:
for team in "${!sumPerTeam[@]}"
do
    echo "$team" "${sumPerTeam[${team}]}"
done | sort -k2

Associative arrays are not Bash's most readable feature.

Comments

0

An awk solution without a separate pipe and subshell to sort could be:

awk '$2 == "a" {arr[$3]} END {for (i in arr) print "a", i}' file

You simply store the age as indexes in an array and then output the indexes at the end which provides an inherent sort without a separate process, e.g.

Example Use/Output

$ awk '$2 == "a" {arr[$3]} END {for (i in arr) print "a", i}' file
a 20
a 24

Calling an external sort, you can reduce the number of characters you type, but then pickup the additional overhead of the pipe as well as the subshell required by sort, e.g.

awk '$2 == "a" {print $2, $3}' file | sort

(same output)

2 Comments

first solution you proposed will only work with GNU awk, other awks don't sort array indices before iteration.
and by calling sort without a key definition you're asking for an alphabetically sorted list of inputs, i.e a 10 will come before a 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.