Calculate the total 4 field based on the conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate the total 4 field based on the conditions
# 15  
Old 11-05-2015
Quote:
Originally Posted by kalia4u
Hi Don,
Many many thanks a lot for ur replay. Issue got resolved
This is the best side never seen before and also i wll recomanded this side to my friends so that they can get help from you gays
Please share usefull docs for awk programming if u have.
I'm glad my replies helped.

You can show your appreciation for posts that helped you solve your problem by hitting the SmilieThanks button in the lower left corner of the post.

A good place to look for documentation for any utility on your system is the man page for that utility. In this case that would mean using the command:
man awk
This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 11-06-2015
Getting issue while print the output header
Code:
print "ACNO|NAME|TOWN|VAL1|VAL2|VAL3|VAL4.........V25"

# 17  
Old 11-06-2015
Quote:
Originally Posted by kalia4u
Getting issue while print the output header
Code:
print "ACNO|NAME|TOWN|VAL1|VAL2|VAL3|VAL4.........V25"

Sorry. I forgot to print the header in the latest revision of the code. Printing a header with a variable number of fields is just like printing a variable number of subtotals or totals.

Change the code that processes the 1st line of input from:
Code:
FNR == 1 {
	# Grab the field count from the 1st input line.
	nf = NF
	# Grab account # and name from the 1st input line.
	acno = $1
	name = $2
}

to also print the header:
Code:
FNR == 1 {
	# Grab the field count from the 1st input line.
	nf = NF
	# Grab account # and name from the 1st input line.
	acno = $1
	name = $2
	# Print the header line.
	printf("ACNO%sNAME%sTOWN%s", OFS, OFS, OFS)
	for(i = 1; i <= nf - 3; i++)
		printf("VAL%d%s", i, (i == nf - 3) ? RS : OFS)
}

# 18  
Old 11-06-2015
Sorry i should mention while this program print the output as per my requirements

Last edited by kalia4u; 11-06-2015 at 08:32 PM..
# 19  
Old 02-19-2016
Hi Don,
some changes in the requirements input file has no header while display output I want to insert header below mentioned
line
ACNO|NAME|TOWN|VAL1|VAL2|VAL3|VAL4|VAL5|VAL6|VAL7|VAL7|VAL9|VAL10.........V25" please help me on this

Last edited by kalia4u; 02-19-2016 at 03:35 PM..
# 20  
Old 02-19-2016
Please show us a sample of the output your script is producing now and an updated sample output including the exact output you now want to produce (both in CODE tags).

And, please show us how you have attempted to modify your script to make it produce the new output you want to add (also in CODE tags).
# 21  
Old 02-23-2016
Hi Don,

I WANT TO INSERT A HEADER .PFA sample file

Code:
#!/bin/ksh
sort -t'|' -k1,1n -k2,2 "${1:-file}" |
awk -F'|' '	
function pt(level,	i) {
	printf("%d%s%s%sTotal%s", acno, OFS, name, OFS, OFS)
	for(i = 4; i <= nf; i++) {
		printf(OFMT "%s", s[i], (i == nf) ? RS : OFS)
		t[i] += s[i]
		s[i] = 0
	}
	if(level) {
		printf("%d%s-%sACNO Total%s", acno, OFS, OFS, OFS)
		for(i = 4; i <= nf; i++) {
			printf(OFMT "%s", t[i], (i == nf) ? RS : OFS)
			t[i] = 0
		}
	}
}
	OFMT = "%.2f"
	OFS = FS
}
FNR == 1 {
	nf = NF
	acno = $1
	name = $2
}
{	
	if(acno != $1) {
		pt(1)
		acno = $1
		name = $2
	}
	if(name != $2) {
		pt(0)
		name = $2
	}
	for(i = 4; i <= nf; i++)
		s[i] += $i
}
1	
END {	
	pt(1)
}'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
Login or Register to Ask a Question