Summing rows in a new column using sed, awk and perl?

Shervan 09/19/2018. 4 answers, 320 views
bash awk sed perl

I have a file that contain numbers something like:

1 11 323
2 13 3
3 44 4
4 66 23
5 70 23
6 34 23
7 24 22
8 27 5

How can I sum the rows and output the results in a column, so the results are as follows:

1 11 323 335
2 13 3 18
3 44 4 51
4 66 23 93
5 70 23 98
6 34 23 63
7 24 22 53
8 27 5 40

I would like to see solutions in sed, awk, and perl

4 Answers


Goro 09/19/2018.

Let's say that your data is saved in a file called data.txt.

cat data.txt
1 11 323
2 13 3
3 44 4
4 66 23
5 70 23
6 34 23
7 24 22
8 27 5

You can do it in awk as follows:

awk '{X=$0}{split(X,x)}{print X , x[1]+x[2]+x[3]}' data.txt
1 11 323 335
2 13 3 18
3 44 4 51
4 66 23 93
5 70 23 98
6 34 23 63
7 24 22 53
8 27 5 40

Or, per @RudiC's comment:

awk '{print $0, $1+$2+$3}' data.txt

choroba 09/19/2018.

Perl solution:

perl -MList::Util=sum -lane 'print "@F ", sum(@F)' < data.txt
  • -n reads the input line by line
  • -l removes newlines from input and adds them to output
  • -a splits each input line on whitespace into the @F array
  • List::Util provides the sum function so you don't have to sum the numbers yourself

In sed, arithmetic is nearly impossible to implement, but you can use sed to turn spaces into pluses and use that as a source for bc to get the sums, and paste the results with the input:

paste -d ' ' data.txt <(sed -r 's/ /+/g' data.txt | bc)

Kusalananda 09/19/2018.

For an arbitrary number of columns, using awk:

$ awk '{ sum = 0; for (i = 1; i <= NF; i++) sum += $i; $(NF + 1) = sum } 1' <file
1 11 323 335
2 13 3 18
3 44 4 51
4 66 23 93
5 70 23 98
6 34 23 63
7 24 22 53
8 27 5 40

NF is the number of fields (whitespace separated columns by default) in the current record (line by default). By calculating sum in a loop and setting $(NF + 1) to the total, we add a new column at the end. This new column is printed along with the others by the lone 1 at the end of the awk script (this may be replaced by { print }).


sed is not really suited for doing any form of arithmetics.


Aaron 09/20/2018.

What about a pure bash solution ?

while read -r a b c; do
    echo $a $b $c $((a+b+c))
done < input

Sample run


HighResolutionMusic.com - Download Hi-Res Songs

1 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
2 Sia

I'm Still Here flac

Sia. 2018. Writer: Sia.
3 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
4 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
5 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
6 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
7 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
8 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
9 Kelsea Ballerini

This Feeling flac

Kelsea Ballerini. 2018. Writer: Andrew Taggart;Alex Pall;Emily Warren.
10 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
11 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
12 Avril Lavigne

Head Above Water flac

Avril Lavigne. 2018. Writer: Stephan Moccio;Travis Clark;Avril Lavigne.
13 Khalid

Better flac

Khalid. 2018. Writer: Charlie Handsome;Jamil Chammas;Denis Kosiak;Tor Erik Hermansen;Mikkel Stoleer Eriksen;Khalid.
14 Lady Gaga

Look What I Found flac

Lady Gaga. 2018. Writer: DJ White Shadow;Nick Monson;Mark Nilan Jr;Lady Gaga.
15 Deep Chills

Run Free flac

Deep Chills. 2018.
16 Dynoro

In My Mind flac

Dynoro. 2018. Writer: Georgi Kay;Feenixpawl;Ivan Gough.
17 Charli XCX

1999 flac

Charli XCX. 2018. Writer: Charli XCX;Troye Sivan;Leland;Oscar Holter;Noonie Bao.
18 NCT 127

Regular (English Version) flac

NCT 127. 2018.
19 Lukas Graham

Love Someone flac

Lukas Graham. 2018. Writer: Don Stefano;Morten "Rissi" Ristorp;Morten "Pilo" Pilegaard;Jaramye Daniels;James Alan;David LaBrel;Lukas Forchhammer Graham.
20 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora.

Related questions

Hot questions

Language

Popular Tags