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
shells(4)							   File Formats 							 shells(4)

NAME
shells - shell database SYNOPSIS
/etc/shells DESCRIPTION
The shells file contains a list of the shells on the system. Applications use this file to determine whether a shell is valid. See getuser- shell(3C). For each shell a single line should be present, consisting of the shell's path, relative to root. A hash mark (#) indicates the beginning of a comment; subsequent characters up to the end of the line are not interpreted by the routines which search the file. Blank lines are also ignored. The following default shells are used by utilities: /bin/bash, /bin/csh, /bin/jsh, /bin/ksh, /bin/ksh93, /bin/pfcsh, /bin/pfksh, /bin/pfsh, /bin/sh, /bin/tcsh, /bin/zsh, /sbin/jsh, /sbin/sh, /usr/bin/bash, /usr/bin/csh, /usr/bin/jsh, /usr/bin/ksh, /usr/bin/ksh93, /usr/bin/pfcsh, /usr/bin/pfksh, /usr/bin/pfsh, and /usr/bin/sh, /usr/bin/tcsh, /usr/bin/zsh, and /usr/sfw/bin/zsh. /etc/shells overrides the default list. Invalid shells in /etc/shells could cause unexpected behavior, such as being unable to log in by way of ftp(1). FILES
/etc/shells list of shells on system SEE ALSO
vipw(1B), ftpd(1M), sendmail(1M), getusershell(3C), aliases(4) SunOS 5.11 20 Nov 2007 shells(4)
All times are GMT -4. The time now is 10:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy