I have a report same as below content:
gold 1 1986 USA Americal Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
silver 10 1981 USA Ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
gold 0.1 1986 PRC Panda
silver 1 1986 USA Liberty dollar
gold 0.25 1986 USA Liberty 5-collar piece
silver 0.5 1986 USA Liberty 50-cent piece
silver 1 1987 USA Constitution dollar
gold 0.25 1987 USA Constitution 5-dollar piece
gold 1 1988 Canada Maple Leaf
I want to extract a report like below:
Gold pieces: 9
Weight of gold pieces: 6.10
Value of gold pieces: 9729.50
this the code I’ve made:
BEGIN{}
$1 ~/^gold$/
{
goldWeight += $2
++goldCounter
}
END{
printf "Gold Pieces:\t\t%d\n" , goldCounter
printf "Weight of gold pieces:\t\t%1.2f\n" , goldWeight
printf "Value of gold pieces:\t\t%1.2f\n" , goldWeight*1771.00
}
the code does not work. where is my problem?
You need the first bracket after the pattern
Then the bit in braces applies to the pattern. Otherwise it applies to every line.