user3848196
user3848196

Reputation: 37

Bash formatting with awk

i am facing difficulties with formatting for bash

This is my existing format, the price is not under the total sales. How can i make it to be place under Total sales? The total sales does not exist in the text file and will only show under certain function. It will not be save into the text file

Title  Author   Price  Qty Avail  Qty Sold  Total Sales
-------------------------------------------------------
Book1  Author1  1      1          1
Book2  Author2  2      2          2
$1.00
$4.00

Below is my coding

echo "" 
column -ts : <(echo Title:Author:Price:Qty Avail:Qty Sold:Total Sales) BookDB.txt | sed "1{p;s/./-/g}"

awk -F : -v search="$1" '        
BEGIN {
count = 0 
}
$1 ~ search {
printf "$%0.2f\n",($3 * $5) 
        ++count
    }
    END {
        printf "%d,records found\n",count
    }
' BookDB.txt | column -t    }

Upvotes: 0

Views: 69

Answers (1)

Mahesh Kharvi
Mahesh Kharvi

Reputation: 399

awk -F : -v search="$1" '
BEGIN {
count = 0;
print "Title:Author:Price:Qty Avail:Qty Sold:Total Sales"
}
sale = 0
$1 ~ search {
        ++count
    }
    {
    sale = $3 * $5
    printf "%s:$%0.2f\n",$0,sale
    }
    END {
        printf "%d records found for %s\n",count,search
    }
' BookDB.txt | column -ts : | sed "1{p;s/./-/g}"

Upvotes: 1

Related Questions