| by Arround The Web | No comments

Calculate the Sum of a Column Using the “Awk” Script in Bash

When the data is stored in a file in a tabular format that contains one or more columns of the numeric data and if it requires to calculate the sum of a column with the numeric data, the “awk” script can be used to do this task. The “awk” script is used in Bash for many purposes. The uses of the “awk” script in Bash to calculate the sum of a column are shown in this tutorial.

Different Examples of Calculating the Sum of Column

The “awk” command can be used in different ways to calculate the sum of a column from a file that contains numeric data. The file can be a CSV file or a text file. Different ways of calculating the sum of a column are shown in this part of the tutorial.

Example 1: Calculate the Sum of a Column Using the Simple “Awk” Command
Create a text file named “courses.txt” with the following data. The second column of this file contains numeric data and the file does not contain any heading data:

CSE202   2.0
CSE407   1.0
CSE305   3.0
ACC101   2.0

Create a Bash file with the following script that takes the filename from the user. If the filename matches with the “courses.txt” file, the sum of the values of the second column from this file is calculated using the “awk” command. The “awk” command is used with the “cat” command here to read the content of the second column of the “courses.txt” file. Then, the summation value is printed.

#!/bin/bash
#Take the filename
echo -n "Enter the filename: "
read filename

#Check the valid filename is given or not
if [[ $filename != "courses.txt" ]]; then
    echo "Invalid filename is given."
    exit 0
fi
#Calculate the sum of total credit hours
total=`cat $filename | awk '{sum+=$2} END {print "Total course credit hours: " sum}'`
#Print the calculated value
echo $total

The following output shows the sum of 2.0+1.0+3.0+2.0 which is 8.0:

Example 2: Calculate the Sum of a Column Using the Simple “Awk” Command with NR
Create a CSV file named “employees.csv” with the following data. The third column of this file contains numeric data and the file contains the heading data:

ID,     Name,             Salary
5623,   Mehrab Hossain,    90000
1355,   Mila Chowdhury,   125000
3517,   Jafar Iqbal,      300000
7554,   Zia Rahman,       450000
8652,   Rupa Chakrobarty, 260000

Create a Bash file with the following script that takes the filename from the command-line argument. If the filename matches with the “employees.csv” file, the sum of the values of the third column excluding the heading from this file is calculated using the “awk” command. The “awk” command uses the “NR” value here to omit the heading of the third column of the file. Then, the summation value is printed.

#!/bin/bash
#Check whether the filename is given or not
if [ $# -lt 1 ]; then
   echo "Argument missing."
   exit 0
fi
filename=$1
#Check whether the valid filename is given or not
if [[ $filename != "employees.csv" ]]; then
    echo "Invalid filename is given."
    exit 0
fi
#Calculate the sum of the Salary field of the employees.csv file
total=`awk -F "," 'NR!=1{Total=Total+$3} END{print "Total salary of the employees is: $" Total}' >#Print the calculated value
echo $total

The script is executed twice in the following output. The script is executed without any argument in the first execution, so the error message is printed. The script is executed with a valid argument value in the second execution, so the summation value is printed:

Example 3: Calculate the Sum of a Column Using the Simple “Awk” Command with FS
Create a text file named “sales.txt” with the following data. The “:” is used in the file to separate the column and the second column of this file that contains the numeric data. The file does not contain any heading data.

Jan:60000
Feb:34000
Mar:120000
Apr:56000
May:65000
Jun:20000

Create a Bash file with the following script that takes the filename from the user. If the filename matches with the “sales.txt” file, the sum of the values of the second column from this file is calculated using the “awk” command. The “awk” command is used with the “FS” value here to define the field separator between the columns of the file. Then, the summation value is printed.

#!/bin/bash
#Take the filename
echo -n "Enter the filename: "
read filename

#Check whether the valid filename is given or not
if [[ $filename != "sales.txt" ]]; then
    echo "Invalid filename is given."
    exit 0
else
    #Print the file content
    cat $filename
fi

#Print the total sales amount using the field separator
cat $filename | awk 'BEGIN{FS=":"; sum=0} {sum+=$2} END{print "Total sales: $" sum}'

The following output appears if “sales.txt” is taken as input:

Conclusion

Multiple ways of calculating the sum of a column from a file using the “awk” command are shown in this tutorial.

Share Button

Source: linuxhint.com

Leave a Reply