Summing rows in a new column using sed, awk and perl?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
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
bash awk sed perl
add a comment |Â
up vote
6
down vote
favorite
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
bash awk sed perl
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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
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
bash awk sed perl
bash awk sed perl
edited Sep 19 at 15:08
Goro
6,16552762
6,16552762
asked Sep 19 at 15:07
Shervan
1669
1669
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
7
down vote
accepted
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=$0split(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
8
Why notawk 'print $0, $1+$2+$3' file
?
â RudiC
Sep 19 at 15:40
how can I do it in sed pelae?
â Shervan
Sep 19 at 15:40
6
sed
is a (stream) editor, not a calculator.
â RudiC
Sep 19 at 15:42
add a comment |Â
up vote
9
down vote
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 thesum
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)
sed is not working. I get this message "Missing name for redirect."
â Shervan
Sep 19 at 15:39
@Shervan Are you sure you're usingbash
?
â Kusalananda
Sep 19 at 15:52
add a comment |Â
up vote
4
down vote
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.
3
@Shervan You can't.sed
does not support arithmetic operations.
â Kusalananda
Sep 19 at 16:00
2
@Shervan And when I say "you can't", I mean that you can, but it would be extremely difficult as you would have to do so with the aid of regular expressions and the other text manipulating facilities thatsed
provides. It would be as difficult to do withsed
as with an ordinary plain text editor that does not support arithmetic operations natively.
â Kusalananda
Sep 19 at 16:04
2
@Shervan See e.g. my solution to solving a sorting problem withsed
: unix.stackexchange.com/a/436251/116858
â Kusalananda
Sep 19 at 16:06
1
@Kusalananda. he can do something like thiscat data | sed -e 's,$, + p,g' | dc
, no? orcat data | sed -e 's/ /+/g' | bc
â Goro
Sep 19 at 16:07
2
@Goro Yes, that would be usingsed
to generate adc
/bc
script. This is different from doing the actual calculation insed
though.
â Kusalananda
Sep 19 at 16:08
 |Â
show 2 more comments
up vote
0
down vote
What about a pure bash solution ?
while read -r a b c; do
echo $a $b $c $((a+b+c))
done < input
Sample run
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
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=$0split(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
8
Why notawk 'print $0, $1+$2+$3' file
?
â RudiC
Sep 19 at 15:40
how can I do it in sed pelae?
â Shervan
Sep 19 at 15:40
6
sed
is a (stream) editor, not a calculator.
â RudiC
Sep 19 at 15:42
add a comment |Â
up vote
7
down vote
accepted
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=$0split(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
8
Why notawk 'print $0, $1+$2+$3' file
?
â RudiC
Sep 19 at 15:40
how can I do it in sed pelae?
â Shervan
Sep 19 at 15:40
6
sed
is a (stream) editor, not a calculator.
â RudiC
Sep 19 at 15:42
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
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=$0split(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
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=$0split(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
edited Sep 19 at 15:43
answered Sep 19 at 15:11
Goro
6,16552762
6,16552762
8
Why notawk 'print $0, $1+$2+$3' file
?
â RudiC
Sep 19 at 15:40
how can I do it in sed pelae?
â Shervan
Sep 19 at 15:40
6
sed
is a (stream) editor, not a calculator.
â RudiC
Sep 19 at 15:42
add a comment |Â
8
Why notawk 'print $0, $1+$2+$3' file
?
â RudiC
Sep 19 at 15:40
how can I do it in sed pelae?
â Shervan
Sep 19 at 15:40
6
sed
is a (stream) editor, not a calculator.
â RudiC
Sep 19 at 15:42
8
8
Why not
awk 'print $0, $1+$2+$3' file
?â RudiC
Sep 19 at 15:40
Why not
awk 'print $0, $1+$2+$3' file
?â RudiC
Sep 19 at 15:40
how can I do it in sed pelae?
â Shervan
Sep 19 at 15:40
how can I do it in sed pelae?
â Shervan
Sep 19 at 15:40
6
6
sed
is a (stream) editor, not a calculator.â RudiC
Sep 19 at 15:42
sed
is a (stream) editor, not a calculator.â RudiC
Sep 19 at 15:42
add a comment |Â
up vote
9
down vote
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 thesum
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)
sed is not working. I get this message "Missing name for redirect."
â Shervan
Sep 19 at 15:39
@Shervan Are you sure you're usingbash
?
â Kusalananda
Sep 19 at 15:52
add a comment |Â
up vote
9
down vote
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 thesum
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)
sed is not working. I get this message "Missing name for redirect."
â Shervan
Sep 19 at 15:39
@Shervan Are you sure you're usingbash
?
â Kusalananda
Sep 19 at 15:52
add a comment |Â
up vote
9
down vote
up vote
9
down vote
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 thesum
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)
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 thesum
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)
edited Sep 19 at 15:19
answered Sep 19 at 15:13
choroba
24.9k34168
24.9k34168
sed is not working. I get this message "Missing name for redirect."
â Shervan
Sep 19 at 15:39
@Shervan Are you sure you're usingbash
?
â Kusalananda
Sep 19 at 15:52
add a comment |Â
sed is not working. I get this message "Missing name for redirect."
â Shervan
Sep 19 at 15:39
@Shervan Are you sure you're usingbash
?
â Kusalananda
Sep 19 at 15:52
sed is not working. I get this message "Missing name for redirect."
â Shervan
Sep 19 at 15:39
sed is not working. I get this message "Missing name for redirect."
â Shervan
Sep 19 at 15:39
@Shervan Are you sure you're using
bash
?â Kusalananda
Sep 19 at 15:52
@Shervan Are you sure you're using
bash
?â Kusalananda
Sep 19 at 15:52
add a comment |Â
up vote
4
down vote
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.
3
@Shervan You can't.sed
does not support arithmetic operations.
â Kusalananda
Sep 19 at 16:00
2
@Shervan And when I say "you can't", I mean that you can, but it would be extremely difficult as you would have to do so with the aid of regular expressions and the other text manipulating facilities thatsed
provides. It would be as difficult to do withsed
as with an ordinary plain text editor that does not support arithmetic operations natively.
â Kusalananda
Sep 19 at 16:04
2
@Shervan See e.g. my solution to solving a sorting problem withsed
: unix.stackexchange.com/a/436251/116858
â Kusalananda
Sep 19 at 16:06
1
@Kusalananda. he can do something like thiscat data | sed -e 's,$, + p,g' | dc
, no? orcat data | sed -e 's/ /+/g' | bc
â Goro
Sep 19 at 16:07
2
@Goro Yes, that would be usingsed
to generate adc
/bc
script. This is different from doing the actual calculation insed
though.
â Kusalananda
Sep 19 at 16:08
 |Â
show 2 more comments
up vote
4
down vote
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.
3
@Shervan You can't.sed
does not support arithmetic operations.
â Kusalananda
Sep 19 at 16:00
2
@Shervan And when I say "you can't", I mean that you can, but it would be extremely difficult as you would have to do so with the aid of regular expressions and the other text manipulating facilities thatsed
provides. It would be as difficult to do withsed
as with an ordinary plain text editor that does not support arithmetic operations natively.
â Kusalananda
Sep 19 at 16:04
2
@Shervan See e.g. my solution to solving a sorting problem withsed
: unix.stackexchange.com/a/436251/116858
â Kusalananda
Sep 19 at 16:06
1
@Kusalananda. he can do something like thiscat data | sed -e 's,$, + p,g' | dc
, no? orcat data | sed -e 's/ /+/g' | bc
â Goro
Sep 19 at 16:07
2
@Goro Yes, that would be usingsed
to generate adc
/bc
script. This is different from doing the actual calculation insed
though.
â Kusalananda
Sep 19 at 16:08
 |Â
show 2 more comments
up vote
4
down vote
up vote
4
down vote
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.
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.
answered Sep 19 at 15:59
Kusalananda
108k14209332
108k14209332
3
@Shervan You can't.sed
does not support arithmetic operations.
â Kusalananda
Sep 19 at 16:00
2
@Shervan And when I say "you can't", I mean that you can, but it would be extremely difficult as you would have to do so with the aid of regular expressions and the other text manipulating facilities thatsed
provides. It would be as difficult to do withsed
as with an ordinary plain text editor that does not support arithmetic operations natively.
â Kusalananda
Sep 19 at 16:04
2
@Shervan See e.g. my solution to solving a sorting problem withsed
: unix.stackexchange.com/a/436251/116858
â Kusalananda
Sep 19 at 16:06
1
@Kusalananda. he can do something like thiscat data | sed -e 's,$, + p,g' | dc
, no? orcat data | sed -e 's/ /+/g' | bc
â Goro
Sep 19 at 16:07
2
@Goro Yes, that would be usingsed
to generate adc
/bc
script. This is different from doing the actual calculation insed
though.
â Kusalananda
Sep 19 at 16:08
 |Â
show 2 more comments
3
@Shervan You can't.sed
does not support arithmetic operations.
â Kusalananda
Sep 19 at 16:00
2
@Shervan And when I say "you can't", I mean that you can, but it would be extremely difficult as you would have to do so with the aid of regular expressions and the other text manipulating facilities thatsed
provides. It would be as difficult to do withsed
as with an ordinary plain text editor that does not support arithmetic operations natively.
â Kusalananda
Sep 19 at 16:04
2
@Shervan See e.g. my solution to solving a sorting problem withsed
: unix.stackexchange.com/a/436251/116858
â Kusalananda
Sep 19 at 16:06
1
@Kusalananda. he can do something like thiscat data | sed -e 's,$, + p,g' | dc
, no? orcat data | sed -e 's/ /+/g' | bc
â Goro
Sep 19 at 16:07
2
@Goro Yes, that would be usingsed
to generate adc
/bc
script. This is different from doing the actual calculation insed
though.
â Kusalananda
Sep 19 at 16:08
3
3
@Shervan You can't.
sed
does not support arithmetic operations.â Kusalananda
Sep 19 at 16:00
@Shervan You can't.
sed
does not support arithmetic operations.â Kusalananda
Sep 19 at 16:00
2
2
@Shervan And when I say "you can't", I mean that you can, but it would be extremely difficult as you would have to do so with the aid of regular expressions and the other text manipulating facilities that
sed
provides. It would be as difficult to do with sed
as with an ordinary plain text editor that does not support arithmetic operations natively.â Kusalananda
Sep 19 at 16:04
@Shervan And when I say "you can't", I mean that you can, but it would be extremely difficult as you would have to do so with the aid of regular expressions and the other text manipulating facilities that
sed
provides. It would be as difficult to do with sed
as with an ordinary plain text editor that does not support arithmetic operations natively.â Kusalananda
Sep 19 at 16:04
2
2
@Shervan See e.g. my solution to solving a sorting problem with
sed
: unix.stackexchange.com/a/436251/116858â Kusalananda
Sep 19 at 16:06
@Shervan See e.g. my solution to solving a sorting problem with
sed
: unix.stackexchange.com/a/436251/116858â Kusalananda
Sep 19 at 16:06
1
1
@Kusalananda. he can do something like this
cat data | sed -e 's,$, + p,g' | dc
, no? or cat data | sed -e 's/ /+/g' | bc
â Goro
Sep 19 at 16:07
@Kusalananda. he can do something like this
cat data | sed -e 's,$, + p,g' | dc
, no? or cat data | sed -e 's/ /+/g' | bc
â Goro
Sep 19 at 16:07
2
2
@Goro Yes, that would be using
sed
to generate a dc
/bc
script. This is different from doing the actual calculation in sed
though.â Kusalananda
Sep 19 at 16:08
@Goro Yes, that would be using
sed
to generate a dc
/bc
script. This is different from doing the actual calculation in sed
though.â Kusalananda
Sep 19 at 16:08
 |Â
show 2 more comments
up vote
0
down vote
What about a pure bash solution ?
while read -r a b c; do
echo $a $b $c $((a+b+c))
done < input
Sample run
add a comment |Â
up vote
0
down vote
What about a pure bash solution ?
while read -r a b c; do
echo $a $b $c $((a+b+c))
done < input
Sample run
add a comment |Â
up vote
0
down vote
up vote
0
down vote
What about a pure bash solution ?
while read -r a b c; do
echo $a $b $c $((a+b+c))
done < input
Sample run
What about a pure bash solution ?
while read -r a b c; do
echo $a $b $c $((a+b+c))
done < input
Sample run
answered Sep 20 at 8:59
Aaron
23819
23819
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470041%2fsumming-rows-in-a-new-column-using-sed-awk-and-perl%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password