HomeLinuxCalculate the Sum of a Column Utilizing the “Awk” Script in Bash

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


When the info is saved in a file in a tabular format that comprises a number of columns of the numeric knowledge and if it requires to calculate the sum of a column with the numeric knowledge, the “awk” script can be utilized to do that job. The “awk” script is utilized in Bash for a lot of functions. The makes use of of the “awk” script in Bash to calculate the sum of a column are proven on this tutorial.

Completely different Examples of Calculating the Sum of Column

The “awk” command can be utilized in several methods to calculate the sum of a column from a file that comprises numeric knowledge. The file is usually a CSV file or a textual content file. Other ways of calculating the sum of a column are proven on this a part of the tutorial.

Instance 1: Calculate the Sum of a Column Utilizing the Easy “Awk” Command
Create a textual content file named “programs.txt” with the next knowledge. The second column of this file comprises numeric knowledge and the file doesn’t comprise any heading knowledge:

CSE202   2.0
CSE407   1.0
CSE305   3.0
ACC101   2.0

Create a Bash file with the next script that takes the filename from the person. If the filename matches with the “programs.txt” file, the sum of the values of the second column from this file is calculated utilizing the “awk” command. The “awk” command is used with the “cat” command right here to learn the content material of the second column of the “programs.txt” file. Then, the summation worth is printed.

#!/bin/bash
#Take the filename
echo -n “Enter the filename: “
learn filename

#Test the legitimate filename is given or not
if [[ $filename != “courses.txt” ]]; then
    echo “Invalid filename is given.”
    exit 0
fi
#Calculate the sum of whole credit score hours
whole=`cat $filename | awk ‘{sum+=$2} END {print “Whole course credit score hours: ” sum}’`
#Print the calculated worth
echo $whole

The next output reveals the sum of two.0+1.0+3.0+2.0 which is 8.0:

Instance 2: Calculate the Sum of a Column Utilizing the Easy “Awk” Command with NR
Create a CSV file named “workers.csv” with the next knowledge. The third column of this file comprises numeric knowledge and the file comprises the heading knowledge:

ID,     Identify,             Wage
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 next script that takes the filename from the command-line argument. If the filename matches with the “workers.csv” file, the sum of the values of the third column excluding the heading from this file is calculated utilizing the “awk” command. The “awk” command makes use of the “NR” worth right here to omit the heading of the third column of the file. Then, the summation worth is printed.

#!/bin/bash
#Test whether or not the filename is given or not
if [ $# -lt 1 ]; then
   echo “Argument lacking.”
   exit 0
fi
filename=$1
#Test whether or not the legitimate filename is given or not
if [[ $filename != “employees.csv” ]]; then
    echo “Invalid filename is given.”
    exit 0
fi
#Calculate the sum of the Wage area of the workers.csv file
whole=`awk -F “,” ‘NR!=1{Whole=Whole+$3} END{print “Whole wage of the workers is: $” Whole}’ >#Print the calculated worth
echo $whole

The script is executed twice within the following output. The script is executed with none argument within the first execution, so the error message is printed. The script is executed with a legitimate argument worth within the second execution, so the summation worth is printed:

Instance 3: Calculate the Sum of a Column Utilizing the Easy “Awk” Command with FS
Create a textual content file named “gross sales.txt” with the next knowledge. The “:” is used within the file to separate the column and the second column of this file that comprises the numeric knowledge. The file doesn’t comprise any heading knowledge.

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

Create a Bash file with the next script that takes the filename from the person. If the filename matches with the “gross sales.txt” file, the sum of the values of the second column from this file is calculated utilizing the “awk” command. The “awk” command is used with the “FS” worth right here to outline the sector separator between the columns of the file. Then, the summation worth is printed.

#!/bin/bash
#Take the filename
echo -n “Enter the filename: “
learn filename

#Test whether or not the legitimate filename is given or not
if [[ $filename != “sales.txt” ]]; then
    echo “Invalid filename is given.”
    exit 0
else
    #Print the file content material
    cat $filename
fi

#Print the whole gross sales quantity utilizing the sector separator
cat $filename | awk ‘BEGIN{FS=”:”; sum=0} {sum+=$2} END{print “Whole gross sales: $” sum}’

The next output seems if “gross sales.txt” is taken as enter:

Conclusion

A number of methods of calculating the sum of a column from a file utilizing the “awk” command are proven on this tutorial.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments