Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7173833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:54:26+00:00 2026-05-28T15:54:26+00:00

My program should be able to work this way. Below is the content of

  • 0

My program should be able to work this way.

Below is the content of the text file named BookDB.txt
The individual are separated by colons(:) and every line in the text file should serve as a set of information and are in the order as stated below.

Title:Author:Price:QtyAvailable:QtySold

Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38

I actually intend to
1) Read the file line by line and store it in an array
2) Display it

However I have no idea on how to even start the first one.
From doing research online, below are the codes which I have written up till now.

#!/bin/bash

function fnReadFile()
{
while read inputline
do
 bTitle="$(echo $inputline | cut -d: -f1)"
 bAuthor="$(echo $inputline | cut -d: -f2)"
 bPrice="$(echo $inputline | cut -d: -f3)"
 bQtyAvail="$(echo $inputline | cut -d: -f4)"
 bQtySold="$(echo $inputline | cut -d: -f5)"
 bookArray[Count]=('$bTitle', '$bAuthor', '$bPrice', '$bQtyAvail', '$bQtySold')
Count = Count + 1
done
}

function fnInventorySummaryReport()
{
fnReadFile
echo "Title                  Author           Price          Qty Avail.      Qty Sold      Total Sales"
for t in "${bookArray[@]}"
do
echo $t
done
echo "Done!"
}

if ! [ -f BookDB.txt ] ; then #check existance of bookdb file, create the file if not exist else continue
touch BookDB.txt
fi

"HERE IT WILL THEN BE THE MENU AND CALLING OF THE FUNCTION"

Thanks to those in advance who helped!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:54:26+00:00Added an answer on May 28, 2026 at 3:54 pm

    Since your goal here seems to be clear, how about using awk as an alternative to using bash arrays? Often using the right tool for the job makes things a lot easier!

    The following awk script should get you something like what you want:

    # This will print your headers, formatted the way you had above, but without
    # the need for explicit spaces.
    BEGIN {
        printf "%-22s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price",
        "Qty Avail.", "Qty Sold", "Total Sales"
    }
    
    # This is described below, and runs for every record (line) of input
    {
        printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f\n",
        substr($1, 1, 22), substr($2, 1, 16), $3, $4, $5, ($3 * $5)
    }
    

    The second section of code (between curly braces) runs for every line of input. printf is for formatted output, and uses the given format string to print out each field, denoted by $1, $2, etc. In awk, these variables are used to access the fields of your record (line, in this case). substr() is used to truncate the output, as shown below, but can easily be removed if you don’t mind the fields not lining up. I assumed “Total Sales” was supposed to be Price multiplied by Qty Sold, but you can update that easily as well.

    Then, you save this file in books.awk invoke this script like so:

    $ awk -F: -f books.awk books
    Title                  Author           Price          Qty Avail.      Qty Sold      Total Sales
    Harry Potter - The Hal J.K Rowling      40.30          10              50            2015.00
    The little Red Riding  Dan Lin          40.80          20              10            408.00
    Harry Potter - The Pho J.K Rowling      50.00          30              20            1000.00
    Harry Potter - The Dea Dan Lin          55.00          33              790           43450.00
    Little Prince          The Prince       15.00          188             9             135.00
    Lord of The Ring       Johnny Dept      56.80          100             38            2158.40
    

    The -F: tells awk that the fields are separated by colon (:), and -f books.awk tells awk what script to run. Your data is held in books.

    Not exactly what you were asking for, but just pointing you toward a (IMO) better tool for this kind of job! awk can be intimidating at first, but it’s amazing for jobs that work on records like this!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a bug in this program where it should be able
I want to be able to play sound files in my program. Where should
I have this program that should execute a piece of code base on the
Consider this problem: I have a program which should fetch (let's say) 100 records
I want to write a program in C++ that should work on Unix and
Which graduate program should I choose – SUNY Buffalo or SUNY Binghamton?
The following line in my C program should provided All/Group/Owner read and write permissions
The assignment at my first year uni computing course says my program should read
If a user types in [[0,0,0], [0,0,1], [1,1,0]] and press enter, the program should
Im writing a program that should read input via stdin, so I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.