Sponsored Content
Top Forums Shell Programming and Scripting How to sum values on specific line number? Post 303025860 by SysAdminRialto on Tuesday 13th of November 2018 10:25:18 PM
Old 11-13-2018
How to sum values on specific line number?

I have a file that looks like this:

Code:
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

Code:
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:

Code:
# 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?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

2. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

3. Shell Programming and Scripting

How would i delete a line at specific line number

Hi guys , I m writing a script to delete a line at particular location. But i m unable to use variable for specifying line number. for example. sed -n '7!p' filename works fine and deletes 7th line from my file but sed -n '$variable!p' filename gives following error. sed: -e... (12 Replies)
Discussion started by: pinga123
12 Replies

4. Shell Programming and Scripting

new line after specific number character

Hi All, I have input file like this: input1: ( 1083479)=T 158V 1798, T 391V 1896,T 1138V 2273,T 1547V 2477,T 2249V 2917,T 3278V 3234,T 4152V 3495,T 5500V 3631, ( 1083501)=T 181V 1851, T 459V 1954,T 810V 2141,T 1188V 2372,T 1638V 2696,T 2731V 3124,T 4799V 3640,... (5 Replies)
Discussion started by: attila
5 Replies

5. Shell Programming and Scripting

write specific line number in file

dear all, i need your advice i have sample script like this: testing.sh for i in {1..10} do echo testing $i done but i forgot create "#!/bin/bash" in above "for" so i want output will like this testing.sh #!/bin/bash for i in {1..10} do echo testing $i done (2 Replies)
Discussion started by: zvtral
2 Replies

6. Shell Programming and Scripting

How To Sum Values Inside One Line In UNIX.?

Dears, Good Day ! Plz; i want to sum-up two consecutive values inside one row, and put the summation in a new column. here the input: 1 2 4 5 6 7 the output should be: 1 2 3 4 5 9 6 7 13 If someone can help me to solve my issue ! thx anyway. Video tutorial on how to use... (5 Replies)
Discussion started by: Ala Alzyadat
5 Replies

7. Shell Programming and Scripting

Cut from specific line number to a line number

Hi All, I've a file like this.. Sheet1 a,1 a,2 a,3 a,4 a,5 Sheet2 a,6 a,7 a,8 a,9 a,10 Sheet3 a,11 a,12 a,13 (7 Replies)
Discussion started by: manab86
7 Replies

8. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

9. Shell Programming and Scripting

How to find sum of any 'n' number of values from file matching target value?

I have a simple text file having payment amount value on each line. At the end of day 'n' number of payments created difference in amount that I need to match from this file. I have information about how many payments created difference and difference amount. Please help me to build shell... (3 Replies)
Discussion started by: swats007
3 Replies

10. Shell Programming and Scripting

Sum values of specific column in multiple files, considering ranges defined in another file

I have a file (let say file B) like this: File B: A1 3 5 A1 7 9 A2 2 5 A3 1 3 The first column defines a filename and the other two define a range in that specific file. In the same directory, I have also three more files (File A1, A2 and A3). Here is 10 sample lines... (3 Replies)
Discussion started by: Bastami
3 Replies
slags2.f(3)							      LAPACK							       slags2.f(3)

NAME
slags2.f - SYNOPSIS
Functions/Subroutines subroutine slags2 (UPPER, A1, A2, A3, B1, B2, B3, CSU, SNU, CSV, SNV, CSQ, SNQ) SLAGS2 Function/Subroutine Documentation subroutine slags2 (logicalUPPER, realA1, realA2, realA3, realB1, realB2, realB3, realCSU, realSNU, realCSV, realSNV, realCSQ, realSNQ) SLAGS2 Purpose: SLAGS2 computes 2-by-2 orthogonal matrices U, V and Q, such that if ( UPPER ) then U**T *A*Q = U**T *( A1 A2 )*Q = ( x 0 ) ( 0 A3 ) ( x x ) and V**T*B*Q = V**T *( B1 B2 )*Q = ( x 0 ) ( 0 B3 ) ( x x ) or if ( .NOT.UPPER ) then U**T *A*Q = U**T *( A1 0 )*Q = ( x x ) ( A2 A3 ) ( 0 x ) and V**T*B*Q = V**T*( B1 0 )*Q = ( x x ) ( B2 B3 ) ( 0 x ) The rows of the transformed A and B are parallel, where U = ( CSU SNU ), V = ( CSV SNV ), Q = ( CSQ SNQ ) ( -SNU CSU ) ( -SNV CSV ) ( -SNQ CSQ ) Z**T denotes the transpose of Z. Parameters: UPPER UPPER is LOGICAL = .TRUE.: the input matrices A and B are upper triangular. = .FALSE.: the input matrices A and B are lower triangular. A1 A1 is REAL A2 A2 is REAL A3 A3 is REAL On entry, A1, A2 and A3 are elements of the input 2-by-2 upper (lower) triangular matrix A. B1 B1 is REAL B2 B2 is REAL B3 B3 is REAL On entry, B1, B2 and B3 are elements of the input 2-by-2 upper (lower) triangular matrix B. CSU CSU is REAL SNU SNU is REAL The desired orthogonal matrix U. CSV CSV is REAL SNV SNV is REAL The desired orthogonal matrix V. CSQ CSQ is REAL SNQ SNQ is REAL The desired orthogonal matrix Q. Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Definition at line 152 of file slags2.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.1 Sun May 26 2013 slags2.f(3)
All times are GMT -4. The time now is 12:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy