Question: I'm trying to generate a summary report in bash. My text file format's this way:

```
title:composer:price:musical pieces available:musical pieces sold

```

However, in my report output's supposed to be this way :

```
Title       Composer   Price   PiecesAvail   PiecesSold   TotalProfit
Graham      Jack       20.00       10            2           40.00
Zack        Squall      4.00       5             3            7.00

```

Anyone can point me in the right direction? I would have to either calculate the total profit beforehand,or while generating the report.

A sample of the textfile's here:

```
 Graham:Jack:20.00:10:2
 Jack:Squall:4.00:5:3
 Random - One:Jim:5.00:4:1
 RainyDay:Melissa:50.00:2:1

```

I've not thought how to calculate the total profit yet though.

Answer: 
Hope this helps:

This script will read the text file and output if hopefully as you'd like..

* Each line from the text file goes into a variable called $line
* it loops over each line
* Using `cut -d ":" -f XX` it cuts a field number (separated by the delimiter `:`) from the $line variable
* I then use bc to do the aritmatic (using `let` will fail as numbers like 20.00 drive it mad)
* then finally it pipes the output through a function and `column` to make it look pretty

**foo.sh:**

```
#!/usr/bin/env bash
echo "title composer price avail sold total"
function output(){
while read line
do
    title="$(cut -d ":"  -f 1 <<<"$line")"
    composer="$(cut -d ":" -f 2 <<<"$line")"
    price="$(cut -d ":" -f 3 <<<"$line")"
    avail="$(cut -d ":" -f 4 <<<"$line")"
    sold="$(cut -d ":" -f 5 <<<"$line")"
    #debug:
    #echo "title:$title, composer:$composer, price=$price, avail=$avail, sold=$sold" 
    total="$(echo "$price * $sold" | bc -l)"
    echo "$title $composer $price $avail $sold $total" 
done < file.txt
}
output | column -t

```

**file.txt**

```
Graham:Jack:20.00:10:2
Zack:Squall:4.00:5:3

```

You'd then call it like this:

```
stark@fourier ~ $ ./foo.sh
title   composer  price  avail  sold  total
Graham  Jack      20.00  10     2     40.00
Zack    Squall    4.00   5      3     12.00

```
Pretty-printing columns can be fiddly, especially if the maximum length of each field is not known in advance. You can use `printf` in awk to do a reasonable enough job though:

```
awk -F: 'BEGIN { printf "%12s%12s%8s%12s%12s\n", "Title","Composer","Price","PiecesAvail","PiecesSold"
}
{ printf "%12s%12s%8s%12s%12s\n",$1,$2,$3,$4,$5 }' file

```

I've specified the colon as the field separator using the switch `-F:`.

The `BEGIN` block prints the headers and every other line is printed using the same format. The format `%Ns` specifies a string padded to `N` characters, so it can be used for fixed width output.

I didn't calculate your total profit but that could be added in as an extra column. If for example it was as simple as `Price * PiecesSolid`, that would be `$3*$5` (column 3 multiplied by column 5). To show it as a number with two decimal places, you could add `%.2f` to the end of the `printf` statement.