Visit Our UNIX and Linux User Community

Search two patterns using awk to print the variable sum

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search two patterns using awk to print the variable sum
# 1  
Old 03-11-2013
Search two patterns using awk to print the variable sum


gold 1 1986 USA American 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-dollar 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

awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt

Output: value = $2592.5

When i tried modifying one of the above programs to calculate and display the total amount (in ounces) of gold and silver, separately but with one program. By using two pairs of pattern/action but it won’t works.

Help me on the above obligation

Last edited by Ramesh M; 03-11-2013 at 11:15 AM..
# 2  
Old 03-11-2013
Set the (value of silver) in this code:
awk '
/^gold/ {gold += $2}
/^silver/ {silver += $2}
END {print "value = $" 425*gold;print "value = $" (value of silver)*silver }' coins.txt

# 3  
Old 03-11-2013
Great Thanks . Its working !!!

Previous Thread | Next Thread
Test Your Knowledge in Computers #532
Difficulty: Medium
UTF-8 uses a fixed number of bytes to encode characters.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

awk - Print Sum

Hi, I have an awk command that I am using, and part of it sums COL_9 however when I read the output it is not including decimal places; awk ' BEGIN{FS=OFS=","} NR==1{print;next} {a+=$9 c = $12 d = $18 } END{for(i in a) {split(i,b,";"); print $1, $2, $3, b, $5, $6, b, b, a, $10, $11,... (1 Reply)
Discussion started by: Ads89
1 Replies

3. Shell Programming and Scripting

awk search patterns from file in another

I would like to grep for aaa and bbb and ccc from one line in file1.txt in any order on a line on file2.txt file1.txt aaa bbb ccc ddd fff ggg hhh ddd jjj jjj cccfile2.txt aaa bbb ccc ddd fff ggg --> output whole line since it matches with aaa bbb ccc of file1.txt aaa ddd jjj hhh --> no... (1 Reply)
Discussion started by: sdf
1 Replies

4. Shell Programming and Scripting

sum from ls -al |awk '{print $5}'

// AIX 5.3 & AIX 6.1 ls -al |awk '{print $5}' This gives each file's size in byte. I need to get: - the sum of all files in Giga bytes with loop. - excluding the size of directories (ls -al returns the size of directories). There are hundreds and thousands of files, so summing up... (8 Replies)
Discussion started by: Daniel Gate
8 Replies

5. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

6. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

7. Shell Programming and Scripting

loop + sum + print using awk

Hi, I am unable sum of each column in the loop usng awk command. Awk is not allowing the parameters in the command. i am facing the below error. awk: 0602-562 Field $() is not correct. Source file abc.txt 100,200,300,400,500,600,700,800,900 101,201,301,401,501,601,701,801,901 ... (1 Reply)
Discussion started by: number10
1 Replies

8. Shell Programming and Scripting

Using a script variable in awk search patterns

Hi all, In a script like : job_date=.... ls -l 2>/dev/null | awk -v var =$job_date ' /Name\.Version\.+\.xml$/ { How can i include a script variable job_date store in "var" in the pattern "/Name\.Version\.+\.xml$/" Thanks in advance (12 Replies)
Discussion started by: abhinav192
12 Replies

9. Shell Programming and Scripting

connecting search patterns in awk with AND

Hello friends, I couldnt connect two search patterns in awk, what i want is to search for two words in a log file; "+MB)" and "Done" i use this code /usr/xpg4/bin/awk '/+MB)/ {gsub("\(","",$5);print int($5)}' mylog.txt and i get integer part of (123,45MB) in a log file "mylog" with ... (1 Reply)
Discussion started by: EAGL€
1 Replies

Featured Tech Videos