How to sum values on specific line number?

I have a file that looks like this:

HP ColorPlotter Z-6100
ACMARTIN IP 192.168.x.x
"VIRTUAL HP  ( C9468A ) PART 1 of 2 (REAL CARTRIDGE 1)"
"VIRTUAL HP  ( C9468A ) PART 2 of 2 (REAL CARTRIDGE 1)"
181
181
"VIRTUAL HP  ( C9471A ) PART 1 of 2 (REAL CARTRIDGE 2)"
"VIRTUAL HP  ( C9471A ) PART 2 of 2 (REAL CARTRIDGE 2)"
274
274
"VIRTUAL HP  ( C9465A ) PART 1 of 2 (REAL CARTRIDGE 3)"
"VIRTUAL HP  ( C9465A ) PART 2 of 2 (REAL CARTRIDGE 3)"
294
294
"VIRTUAL HP  ( C9464A ) PART 1 of 2 (REAL CARTRIDGE 4)"
"VIRTUAL HP  ( C9464A ) PART 2 of 2 (REAL CARTRIDGE 4)"
154
154
"VIRTUAL HP  ( C9469A ) PART 1 of 2 (REAL CARTRIDGE 5)"
"VIRTUAL HP  ( C9469A ) PART 2 of 2 (REAL CARTRIDGE 5)"
112
112
"VIRTUAL HP  ( C9470A ) PART 1 of 2 (REAL CARTRIDGE 6)"
"VIRTUAL HP  ( C9470A ) PART 2 of 2 (REAL CARTRIDGE 6)"
290
290
"VIRTUAL HP  ( C9466A ) PART 1 of 2 (REAL CARTRIDGE 7)"
"VIRTUAL HP  ( C9466A ) PART 2 of 2 (REAL CARTRIDGE 7)"
340
340
"VIRTUAL HP  ( C9467A ) PART 1 of 2 (REAL CARTRIDGE 8)"
"VIRTUAL HP  ( C9467A ) PART 2 of 2 (REAL CARTRIDGE 8)"
282
282

Now I want to sum the values and have the file look like this

HP ColorPlotter Z-6100
ACMARTIN IP 192.168.x.x
"VIRTUAL HP  ( C9468A ) PART 1 of 2 (REAL CARTRIDGE 1)"
"VIRTUAL HP  ( C9468A ) PART 2 of 2 (REAL CARTRIDGE 1)"
362
"VIRTUAL HP  ( C9471A ) PART 1 of 2 (REAL CARTRIDGE 2)"
"VIRTUAL HP  ( C9471A ) PART 2 of 2 (REAL CARTRIDGE 2)"
548
"VIRTUAL HP  ( C9465A ) PART 1 of 2 (REAL CARTRIDGE 3)"
"VIRTUAL HP  ( C9465A ) PART 2 of 2 (REAL CARTRIDGE 3)"
588
"VIRTUAL HP  ( C9464A ) PART 1 of 2 (REAL CARTRIDGE 4)"
"VIRTUAL HP  ( C9464A ) PART 2 of 2 (REAL CARTRIDGE 4)"
308
"VIRTUAL HP  ( C9469A ) PART 1 of 2 (REAL CARTRIDGE 5)"
"VIRTUAL HP  ( C9469A ) PART 2 of 2 (REAL CARTRIDGE 5)"
224
"VIRTUAL HP  ( C9470A ) PART 1 of 2 (REAL CARTRIDGE 6)"
"VIRTUAL HP  ( C9470A ) PART 2 of 2 (REAL CARTRIDGE 6)"
580
"VIRTUAL HP  ( C9466A ) PART 1 of 2 (REAL CARTRIDGE 7)"
"VIRTUAL HP  ( C9466A ) PART 2 of 2 (REAL CARTRIDGE 7)"
680
"VIRTUAL HP  ( C9467A ) PART 1 of 2 (REAL CARTRIDGE 8)"
"VIRTUAL HP  ( C9467A ) PART 2 of 2 (REAL CARTRIDGE 8)"
564

I can only compute the first two values and send output to a specific line in a file which is what I want to accomplish. I did this by using these commands:

# Sum two values and send answer to a file
a=$( awk 'NR==5{a=$0}NR==6{print $0+a}' file1 

# Place var a answer to line 7
sed "7s/^/$a\n/" file1 > file2

# Remove lines 5 and 6
sed -i '5,6d' file2

but I don't know how to compute the rest of the values.

Is there a way to sum the values and delete the values after outputting the answer using awk?

You haven't told us what shell or operating system you're using.

The following should do what you want with file2 containing all of the summed values from adjacent lines that only contain one field that are found in file1 :

#!/bin/ksh
awk '
NF == 1 {
	found = 1
	sum += $1
	next
}
found {	print sum
	found = sum = 0
}
1
END {	if(found)
		print sum
}' file1 > file2

This example uses ksh but should work with any shell that follows Bourne shell or C shell syntax.

If you want to try this on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk or nawk .

1 Like

Try also (for a data sample exactly as in post#1):

awk 'NF==1 {getline X; $0+=X} 1' file
1 Like

Hi Don Cragun,

Thank you for replying to my post. I'm using Bash Shell on CentOS 7.

Thank you RudiC, your command worked great!!

------ Post updated at 12:10 PM ------

Thank you Don Cragun. Your awk command worked in Bash Shell! Thanks for all your help!!