---
title: "Bash: How to Calculate Sum After Reading File"
description: "Here is a shell script to calculate the sum of input file records. And to replace a string with a new one using sed utility."
url: https://srinimf.com/2021/11/15/bash-how-to-calculate-sum-after-reading-file/
date: 2021-11-15
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2021/11/pexels-photo-2411520.jpeg?fit=1880%2C1253&ssl=1
categories: ["Shell scripts"]
tags: ["Sed in Shell script", "Sed with file processing"]
type: post
lang: en
---

# Bash: How to Calculate Sum After Reading File

Here’s a bash script that reads an input file and calculates the sum. A file containing a couple of records is an input for this script. The script reads this file using While (loop) and checks for a specific string; If found, further, it gets a substring of numbers for some calculation.

*Bash script reading input file*

## Reading file and calcuate sum

- The **read – r** command reads the file. The While loop reads till the end of the file. Added if logic to test the string.
- The **sed utility** formats the input record as desired. When the matched string is received, it will then get a number (used substring concept). Then, the echo statement displays the calculated sum.

## Input file

```
app1:
cpu:1500m
mem:10
app2:
cpu:200m
mem:20
```

## Script logic

The result would be the [sum of](https://srinimf.com/2021/11/22/how-to-find-sum-of-series-in-python/) app1 memory and app2 memory. It gives 10+20=30.

![script logic with sed utility usage](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/11/bash-sed.png?resize=713%2C489&ssl=1)

### What it does?

- It replaces the ‘m’ that is found in the ***cpu***. Also, it calculates the sum of the mem of app1 and app2 values.
- The read -r line reads input. The file name inputfile.txt works as an input to the While loop. The sed output, it writes to Output.txt file.
- When the string matches to ‘mem’ it gets a number from it. This is later will use it to calculate the sum.
- This is a tricky shell script and may ask in interviews.
- The >> says it appends to output.txt file. If you use > each time it overwrites.

**Result**

Here’s the output from this script. You can see a display of the sum. Also, you can see the output file details without ‘m’ for ***cpu*** value.

![Output of sed utility in script](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/11/result-sed.png?resize=720%2C264&ssl=1)

[Heart-beat monitor](https://amzn.to/3CCDX8J)

Related posts

- ## [How to Find Databricks Job Status, Owner, and Active Runs Fast](https://srinimf.com/2026/10/17/databricks-find-job-status/) Need to audit Databricks workflows? Step-by-step guide to tracking job creators, service principal run identities, and current execution status in Databricks. October 17, 2026
- ## [30 SQL Practice Queries with Solutions — Run Them Live](https://srinimf.com/2026/10/01/30-sql-practice-queries-with-solutions-run-them-live/) Practice 30 SQL queries with solutions, from SELECT and JOINs to subqueries and window functions. Run SQL queries live in an interactive SQL Playground. October 1, 2026
- ## [Supercharge Your Database Skills: Try Our New Interactive SQL Plugin!](https://srinimf.com/2026/09/30/supercharge-your-database-skills-try-our-new-interactive-sql-plugin/) Struggling with database queries? Explore our new interactive SQL plugin to write, test, and master SQL directly in your browser. Try the live demo now! September 30, 2026

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2021%2F11%2F15%2Fbash-how-to-calculate-sum-after-reading-file%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2021%2F11%2Fpexels-photo-2411520.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Bash%3A%20How%20to%20Calculate%20Sum%20After%20Reading%20File)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2021/11/15/bash-how-to-calculate-sum-after-reading-file/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2021/11/15/bash-how-to-calculate-sum-after-reading-file/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2021/11/15/bash-how-to-calculate-sum-after-reading-file/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2021/11/15/bash-how-to-calculate-sum-after-reading-file/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Bash%3A%20How%20to%20Calculate%20Sum%20After%20Reading%20File&body=https%3A%2F%2Fsrinimf.com%2F2021%2F11%2F15%2Fbash-how-to-calculate-sum-after-reading-file%2F&share=email)
-

### Like this:

Like Loading…
