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 app1 memory and app2 memory. It gives 10+20=30.

script logic with sed utility usage

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

Heart-beat monitor

Related posts

  • How to Create a Generic Stored Procedure for KPI Calculation (SQL + AWS Lambda)

    In modern data engineering, building scalable and reusable systems is essential. Writing separate SQL queries for every KPI quickly becomes messy and hard to maintain. A better approach?👉 Use a Generic Stored Procedure powered by Dynamic SQL, and trigger it using AWS Lambda. In this blog, you’ll learn: What is a Generic Stored Procedure? A…

  • Unlocking the Power of Databricks Genie: A Comprehensive Guide

    Databricks Genie is a collaborative data engineering tool built on the Databricks Unified Analytics Platform, enhancing data analytics for businesses. Key features include collaborative workspaces, efficient data processing with Apache Spark, built-in machine learning capabilities, robust data visualization, seamless integration, and strong security measures, fostering informed decision-making.

  • Secure S3 File Upload Using API Gateway, Lambda & PostgreSQL (Complete AWS Architecture Guide

    Modern applications often allow users to upload files—documents, invoices, images, or datasets. But a production-grade upload pipeline must be secure, scalable, and well-organized. In this article, we will build a complete end-to-end architecture where: We will implement this using Amazon API Gateway, AWS Lambda, PostgreSQL, and Amazon S3. This architecture is widely used in cloud-native…