Adding and sorting values in a Bash script

Related searches

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  
awk '$2==team{print $2,$3}' team='a' file | sort -t' ' -k2n

Sorting an array in Bash using Bubble sort, Bash shell script to find sum of digits � Programs for printing different patterns in Bash � Bash shell script to swap two numbers � Basic Operators in� Prerequisite : Bubble Sort Given an array arr sort the array in ascending order using bash scripting. Examples: Input : 9 7 2 5 Output : Array in sorted order : 2 5 7 9

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 |
+------+----------+

Sorting files using a bash script, The two "inplace" functions attempt to save a few bytes by creating only one array (and some one-off variables for looping and swapping values);� $ sort -nr filename.txt. Command : $ sort -nr file1.txt Output : 200 89 50 39 15 -k Option : Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column. Example : Let us create a table with 2 columns

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.

Bash sort command – Linux Hint, Note that in addition to containing values 1 through 100, the list also includes ' 9e12′ (900) and '99K' (99000). Command line. unsorted-numeric-values | sort - n. For the above script, we have to run as bash add.sh in the Terminal. bash add.sh. Click Enter to display the output of adding two numbers. Output: The below output will be displayed after executing the Script in the Terminal. So we got the output as 100 by adding two input values 55 and 45.

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)

sort (Unix), In computing, sort is a standard command line program of Unix and Unix-like operating systems Sorting a file with tab separated values requires a tab character to be specified as the column delimiter. This illustration uses the shell's dollar-quote notation to specify the tab as a C escape sequence. $ sort -k2,2 -t $'\t '� Example -1: Sending three numeric values as arguments. Create a bash file and add the following code. The script will receive three argument values and store in $1, $2 and $3. It will count the total number of arguments, print argument values with loop and without loop. Lastly, print the sum of all argument values.

Linux sort command help and examples, Compare human readable numbers (e.g., "2K", "1G"). -n, --numeric-sort, Compare according to string numerical value. -R, --random-sort, Sort by� script_one.sh prints the values of the variables, which are alpha and bravo. script_two.sh prints the values of the variables (alpha and bravo) as it received them. script_two.sh changes them to charlie and delta. script_one.sh prints the values of the variables, which are still alpha and bravo.

Sort is an external command that concatenates files while sorting their contents according to a sort type and writes results of sort to standard output. Sort command options for bash. The sort command comes with 31 options (13 main and 18 categorized as other). Most experienced bash programming (even experts) know only a few main sort options

Write a Bash script so that it receives arguments that are specified when the script is called from the command line. Use this method when a script has to perform a slightly different function depending on the values of the input parameters, also called arguments.

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)
  • How does this answer the OP (i.e. "in a Bash script")?
  • 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.