Sponsored Content
Top Forums Shell Programming and Scripting Calculate the total 4 field based on the conditions Post 302959506 by Don Cragun on Tuesday 3rd of November 2015 09:13:34 PM
Old 11-03-2015
Maybe this will help?
Code:
#!/bin/ksh
IAm=${0##*/}			# Get basename of this script.
tmpf="$IAm.$$"			# Create name for temp file to be used to sort
				# keys.
awk -F'|' -v tmpf="$tmpf" '	# Use awk to evaluate the following wcript with
				# FS (the input field separator) set to "|" and
				# with the awk variable tmpf set to the value of
				# the shell variable with the same name.
# Define function to print account number totals and clear the accumulated
# totals.
function pt() {
	# This function will be called whenever the 1st field (account #)
	# changes.  So, we do not want to print anything the first time we are
	# called.
	if(printed++) {
		# Print account total.
		printf("%-*s" FS OFMT FS OFMT FS OFMT FS OFMT "\n",
		    lw, "ACNO Total", t1, t2, t3, t4)
		# Clear totals in preparation for next account.
		t1 = t2 = t3 = t4 = 0
	}
}
{	# For each linee read from the input file(s):
	#	set key to the account number "|" name,
	#	set v1[key], v2[key], v3[key], and v4[key] to the sum of the 
	#		values seen for the corresponding fields so far,
	#	increment the number of lines seen with this key (linec[key]),
	#	save the input line so it can be printed as is with other lines
	#		for this key,
	#	and keep track of the widest combination of key and town seen
	#		for this key.  The totalwidth[key] value will be used
	#		align the values in the account total output with the
	#		values on the widest line for that key.
	v1[key = $1 FS $2] += $4
	v2[key] += $5
	v3[key] += $6
	v4[key] += $7
	line[key, ++linec[key]] = $0
	if(length(key FS $3) > totalwidth[key])
		totalwidth[key] = length(key FS $3)
}
END {	# After we have read all of the input file(s) given, process the data we
	# have accumulated.
	if(NR == 0)
		# If no input lines were found, we are done.
		exit
	# Sort the list of keys found in the input file(s) using the account
	# number as a numeric value as the primary key and the name as the
	# secondary key saving the sorted output in the temporary file named by
	# tmpf.
	sort_cmd = "sort -t\"|\" -k1,1n -k2,2 > " tmpf
	for(key in v1)
		print key | sort_cmd
	# Tell the system we have written all of the keys we have, to sort the
	# keys we have written, and to save the results in our temporary file.
	close(sort_cmd)
	# Print the output file header.
	print "ACNO|NAME|TOWN|VAL1|VAL2|VAL3|VAL4"
	# Read the keys from the (sorted) temporary file (still using "|" as
	# the field separator).
	while((rc = (getline < tmpf)) == 1) {
		# If the account # on this line is not the same as the account #
		# seen on the previous line, print the totals for the previous
		# account # and save the new account #.
		if ($1 != acno) {
			pt()
			acno = $1
		}
		# Save the key.  (We could just use $0, but key is more
		# descriptive.)
		key = $0
		# Print all of the input lines found for this key in the order
		# in which they appeared in the input file(s).
		for(i = 1; i <= linec[key]; i++)
			print line[key, i]
		# Print the sums for this key.
		printf("%-*s" FS OFMT FS OFMT FS OFMT FS OFMT "\n",
		    lw = totalwidth[key], "Total",
		    v1[key], v2[key], v3[key], v4[key])
		# Add the sums for this key to the sums for this account #.
		t1 += v1[key]
		t2 += v2[key]
		t3 += v3[key]
		t4 += v4[key]
	}
	# Tell the system we are done with the temporary file.
	close(tmpf)
	# And, print the total line for the last account # we encountered.
	pt()
}' file # End the awk script and list the file(s) to be processed.
# Remove the temporary file we created.
rm -f "$tmpf"

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to calculate total

Hi First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears. I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines... (2 Replies)
Discussion started by: appsguy616
2 Replies

2. Shell Programming and Scripting

Calculate total sum from a file

The file content is dynamic and using this format: name1 number1 name2 number2 name3 number3 name4 number4 .................... Need a smooth way to calculate the sum of all the numbers in that file (number1 + number2 + number3 + number4........ = total ) (11 Replies)
Discussion started by: TehOne
11 Replies

3. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

4. Shell Programming and Scripting

Extract file records based on some field conditions

Hello Friends, I have a file(InputFile.csv) with the following columns(the columns are pipe-delimited): ColA|ColB|ColC|ColD|ColE|ColF Now for this file, I have to get those records which fulfil the following condition: If "ColB" is NOT NULL and "ColD" has values one of the following... (9 Replies)
Discussion started by: mehimadri
9 Replies

5. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

6. Shell Programming and Scripting

Calculate total value from a row

HI I have a file # cat marks.txt MARKS LIST 2013 Name english french chinese latin total_marks wer 34 45 67 23 wqa 12 39 10 56 wsy 23 90 23 78 Now i need to find the total marks of each student using... (11 Replies)
Discussion started by: Priya Amaresh
11 Replies

7. Shell Programming and Scripting

Calculate the total

Hi All , I have the following script as below , I tried to modify to meet the requirement , could someone help ? very thanks ================================================================================================ while read STR NAME; do Total=0 MyString="$STR" GetData () {... (18 Replies)
Discussion started by: ust3
18 Replies

8. Shell Programming and Scripting

awk to calculate total and percent off field in file

Trying to use awk to print the lines in file that have either REF or SNV in $3, add a header line, sort by $4 in numerical order. The below code does that already, but where I am stuck is on the last part where the total lines are counted and printed under Total_Targets, under Targets_less_than is... (4 Replies)
Discussion started by: cmccabe
4 Replies

9. Shell Programming and Scripting

awk to assign points to variables based on conditions and update specific field

I have been reading old posts and trying to come up with a solution for the below: Use a tab-delimited input file to assign point to variables that are used to update a specific field, Rank. I really couldn't find too much in the way of assigning points to variable, but made an attempt at an awk... (4 Replies)
Discussion started by: cmccabe
4 Replies
GRIB_COMPARE(1) 						   User Commands						   GRIB_COMPARE(1)

NAME
grib_compare - Compares the grib messages contained in two files. DESCRIPTION
Compares the grib messages contained in two files one by one in the same order. If some differences are found it fails returning an error code. All the keys are compared except those listed with the -b option and those that are specific of a different grib edition. Also data values are compared and are considered as different if their maximum absolute difference is greater than the absolute error, that by default is 0.000001. The value used by the absolute error can be set with te -e option. USAGE
grib_compare [options] grib_file grib_file OPTIONS
-b key,key,... Black list. All the keys in this list are skipped when comparing the two files. -c key,key,... Keys to compare. Only the listed keys are compared. -e tolerance Only values whose difference is more than tolerance are considered different. -w key[:{s/d/l}]{=/!=}value,key[:{s/d/l}]{=/!=}value,... Where clause. Grib messages are processed only if they match all the key/value constraints. A valid constraint is of type key=value or key!=value. For each key a string (key:s) or a double (key:d) or a long (key:l) type can be specified. Default type is string. -f Force. Force the execution not to fail on error. -V Version. -7 Does not fail when the message has wrong length -v Verbose. AUTHOR
This manpage has been autogenerated by Enrico Zini <enrico@debian.org>from the command line help of grib_compare. grib_compare April 2009 GRIB_COMPARE(1)
All times are GMT -4. The time now is 05:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy