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
SYSPROFILE(8)						      System Manager's Manual						     SYSPROFILE(8)

NAME
sysprofile - modular centralized shell configuration DESCRIPTION
sysprofile is a generic approach to configure shell settings in a modular and centralized way mostly aimed at avoiding work for lazy sysad- mins. It has only been tested to work with the bash shell. It basically consists of the small /etc/sysprofile shell script which invokes other small shell scripts having a .bash suffix which are contained in the /etc/sysprofile.d/ directory. The system administrator can drop in any script he wants without any naming convention other than that the scripts need to have a .bash suffix to enable automagic sourcing by /etc/sysprofile. This mechanism is set up by inserting a small shell routine into /etc/profile for login shells and optionally into /etc/bashrc and/or /etc/bash.bashrc for non-login shells from where the actual /etc/sysprofile script is invoked: if [ -f /etc/sysprofile ]; then . /etc/sysprofile fi For using "sysprofile" under X11, one can source it in a similar way from /etc/X11/Xsession or your X display manager's Xsession file to provide the same shell environment as under the console in X11. See the example files in /usr/share/doc/sysprofile/ for illustration. For usage of terminal emulators with a non-login bash shell under X11, take care to enable sysprofile via /etc/bash.bashrc. If not set this way, your terminal emulators won't come up with the environment defined by the scripts in /etc/sysprofile.d/. Users not wanting /etc/sysprofile to be sourced for their environment can easily disable it's automatic mechanism. It can be disabled by simply creating an empty file called $HOME/.nosysprofile in the user's home directory using e.g. the touch(1) command. Any single configuration file in /etc/sysprofile.d/ can be overridden by any user by creating a private $HOME/.sysprofile.d/ directory which may contain a user's own version of any configuration file to be sourced instead of the system default. It's names have just to match exactly the system's default /etc/sysprofile.d/ configuration files. Empty versions of these files contained in the $HOME/.syspro- file.d/ directory automatically disable sourcing of the system wide version. Naturally, users can add and include their own private script inventions to be automagically executed by /etc/sysprofile at login time. OPTIONS
There are no options other than those dictated by shell conventions. Anything is defined within the configuration scripts themselves. SEE ALSO
The README files and configuration examples contained in /etc/sysprofile.d/ and the manual pages bash(1), xdm(1x), xdm.options(5), and wdm(1x). Recommended further reading is everything related with shell programming. If you need a similar mechanism for executing code at logout time check out the related package syslogout(8) which is a very close compan- ion to sysprofile. BUGS
sysprofile in its current form is mainly restricted to bash(1) syntax. In fact it is actually a rather embarrassing quick and dirty hack than anything else - but it works. It serves the practical need to enable a centralized bash configuration until something better becomes available. Your constructive criticism in making this into something better" is very welcome. Before i forget to mention it: we take patches... ;-) AUTHOR
sysprofile was developed by Paul Seelig <pseelig@debian.org> specifically for the Debian GNU/Linux system. Feel free to port it to and use it anywhere else under the conditions of either the GNU public license or the BSD license or both. Better yet, please help to make it into something more worthwhile than it currently is. SYSPROFILE(8)
All times are GMT -4. The time now is 03:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy