add lines for each 3 lines

This question already has an answer here:

  • Sum values in 5th column that correspond to same field in 2nd column 3 answers

I have a document like this

1 0.2 1 0.3 1 0.1 2 0.8 2 0.5 2 0.3 

I want to add the lines if the first column are the same and output something like this

1 0.6 2 1.6 

How can I do this with awk? Or maybe it is easier with other tools?

PD: the first column are the same for each 3 lines always, so maybe make an array that adds $2 until line count goes to 3 and print? something like this awk 'BEGIN {x=0,n=1} {n++,x=x+$3}'but with some condition?


We can build an array of "first value, sum of second value" and then after the whole file has been read in we can print out the results

awk '{a[$1]+=$2} END { for (v in a) { print v,a[v]}}' datafile

If you want the output sorted then the easiest way is to pipe that through sort:

awk '{a[$1]+=$2} END { for (v in a) { print v,a[v]}}' datafile | sort -n

I'd do it with perl:

#!/usr/bin/env perl

use strict;
use warnings 'all'; 

my %sum_of;
while ( <DATA> ) {
   my ( $key, $value ) = m/(\d+) ([\d\.]+)/;
   $sum_of{$key} += $value;

print "$_ $sum_of{$_}\n" for sort keys %sum_of;

1 0.2
1 0.3
1 0.1
2 0.8
2 0.5
2 0.3

This gives the desired output.

As a one liner:

perl -lne '($key, $value) = split; $sum_of{$key}+=$value; END {print "$_ $sum_of{$_}" for sort keys %sum_of}'

Category: text processing Time: 2016-07-28 Views: 1

Related post

  • Applying features and add-ons for multiple lines 2014-08-13

    I want the user to have the ability to add features and add-ons for each line on the website (order page). If you look at the UI wireframe, it will ask you how many lines do you want from a drop-down. In this example I have selected two and which wil

  • Obtain \badness or glue adjustment for each line 2014-07-09

    This might be interesting for perfectionists and/or fastidious typesetters who would like to improve the document even further (beyond the beyond the magnificence of a book with zero bad boxes). We all know that the hyphenation algorithm, as conceive

  • How to retrieve start and end points for each line in line shapefile? 2015-05-06

    I have a vector(polyline) shapefile and I need to retrieve each line´s start and end points(nodes) and create a shapefile with these points. Then I need to add to the polyline´s attribute table the start and end point for each line based in the point

  • all occurences of each line from a file in another file for each line in the first file 2012-03-08

    This is a code golf problem: Say you have two files, one file s and one file h. The problem is that for each line l of s you should produce a list that contains all lines of h that contain l. By "contain" I mean a substring, so, for example the

  • How to trim the end of line character for each line read from an external file? 2012-09-25

    How to trim the end of line character for each line read from an external \jobname.list file below? Note that the real scenario does not use filecontents package to generate the list. I use a batch file to prepare the list. The following MWE is just

  • BASH - Run command for each line in output of previous command 2012-10-24

    All, I am want to request all network services using: networksetup -listallnetworkservices I then want to run the below command for each line in produced from the above command: networksetup -setautoproxyurl "A LINE FROM ABOVE" http://etc... Adi

  • find/grep outputs two lines for each file it finds match 2014-02-06

    I search my code base to find string patterns and list all the files where the pattern is found but only the file and not the lines on which it is found: find . -name *.java -exec grep -l 'someText' {} \; -print 2>/dev/null However, it outputs two li

  • Display google map with boundary lines for each address 2015-06-16

    I am working on Google MAP API integration and I have tried to display the map with boundary lines for each address, such as Eg:,-80.147139,25.9432,-80.152374_rect/17_zm/ Zillow g

  • execute a command for each line of stdin immediately? 2015-12-22

    How do I execute a command for each line of stdin immediately? Example: (echo abc; sleep 10; echo def;) | ??? The xargs tool does not do the job because it blocks until it sees all the data (waits for stdout to close). Yet I have to execute commands

  • Replace first six commas for each line in a text file 2016-02-15

    I want to replace the first six , for each line in a text file using sed or something similar in linux. There are more than six , on each line, but only the first six should be replaced by |. --------------Solutions------------- Sed doesn't really su

  • How to replace one line for multiple lines in a file, programatically? 2013-02-01

    I have this config file I want to edit programatically. I always use sed to make small changes, but now I need to replace 1 line for multiple lines, something like this: some code # maintenance placeholder more code Change it into this some code main

  • Put a new string of characters on a new line after each line break after every new string in a text file 2015-08-26

    I need to put a new string of characters on a new line after each line break after every new string in a text file. It is a large list of website addresses that are only seperated by one space on one continuous line but each web address needs to be o

  • Can screen add nl (new-line) for each (carriage-return) 2015-05-05

    Edited: I have data acquisition hardware that sends out serial data via UART into FTDI (serial to USB) device. The FTDI is plugged into USB port and its port name is /dev/ttyUSB0. This device outputs data in the following format: data line 1\r data l

  • Regex to find and add sequence reseting for each line? 2016-01-14

    At textpad I have a text file and I need to find every <@> in every line (from the start of the line) and add a sequence number inside it but the sequence to end at the end of each line.. (the next line sequence will start again from 1). Example: bl

  • All images on one line for each post 2011-07-26

    Having trouble getting around the issue where my client wants all of the images for each post to be on one line. This can be done in the editor, but they are not very tech savvy -- just wants to be able to type a blurb, and then add a series of photo

  • awk for loop for each line in a file 2014-06-12

    I'm a newbie to bash scripting. I need help trying to figure out how to get loop through a text file and for every line take the fields of name , project #, and email, and replace them in a email template to be sent out. So this is the text file call

  • Concatenate filename inside of CSV file for each line for multiple CSV files 2014-06-25

    I have lots of CSV files and I need the filename of each specific CSV file in every line of each file. Original file content of abc123.csv: ColVal_1;ColVal_2;ColVal_3 ColVal_4;ColVal_5;ColVal_6 New file content: ColVal_1;ColVal_3;ColVal_3;abc123.csv

  • How to draw a vertical line for each event in a TikZ chronology diagram? 2015-02-04

    I am using the Tikz chronology package from Levi Wiseman. Specifically, I have used the code presented here and added some own commands in order to avoid rotation, etc. However, I would like to add vertical lines connecting each event date with the r

  • For each line in a file, read two integers and output the minimum 2015-05-11

    I am going through some CodingAbbey problems to learn basic programming tasks, with a focus on taking input from a file, and saving to another. I have just completed this task: Of two numbers, please, select one with minimum value. Here are several p

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 1.262 (s). 13 q(s)