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
LIBBASH(7)							  libbash Manual							LIBBASH(7)

NAME
libbash -- A bash shared libraries package. DESCRIPTION
libbash is a package that enables bash dynamic-like shared libraries. Actually its a tool for managing bash scripts whose functions you may want to load and use in scripts of your own. It contains a 'dynamic loader' for the shared libraries ( ldbash(1)), a configuration tool (ldbashconfig(8)), and some libraries. Using ldbash(1) you are able to load loadable bash libraries, such as getopts(1) and hashstash(1). A bash shared library that can be loaded using ldbash(1) must answer 4 requirments: 1. It must be installed in $LIBBASH_PREFIX/lib/bash (default is /usr/lib/bash). 2. It must contain a line that begins with '#EXPORT='. That line will contain (after the '=') a list of functions that the library exports. I.e. all the function that will be usable after loading that library will be listed in that line. 3. It must contain a line that begins with '#REQUIRE='. That line will contain (after the '=') a list of bash libraries that are required for our library. I.e. every bash library that is in use in our bash library must be listed there. 4. The library must be listed (For more information, see ldbashconfig(8)). Basic guidelines for writing library of your own: 1. Be aware, that your library will be actually sourced. So, basically, it should contain (i.e define) only functions. 2. Try to declare all variables intended for internal use as local. 3. Global variables and functions that are intended for internal use (i.e are not defined in '#EXPORT=') should begin with: __<library_name>_ For example, internal function myfoosort of hashstash library should be named as __hashstash_myfoosort This helps to avoid conflicts in global name space when using libraries that come from different vendors. 4. See html manual for full version of this guide. AUTHORS
Hai Zaar <haizaar@haizaar.com> Gil Ran <ril@ran4.net> SEE ALSO
ldbash(1), ldbashconfig(8), getopts(1), hashstash(1) colors(1) messages(1) urlcoding(1) locks(1) Linux Epoch Linux
All times are GMT -4. The time now is 09:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy