How do I add numbers from two txt files with Bash?
I have a txt file that contains some numbers like this:
1
2
3
4
5
And I have another txt file that contains the same number of lines, but with other numbers:
6
7
8
9
10
I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?
bash shell-script text-processing numeric-data
New contributor
add a comment |
I have a txt file that contains some numbers like this:
1
2
3
4
5
And I have another txt file that contains the same number of lines, but with other numbers:
6
7
8
9
10
I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?
bash shell-script text-processing numeric-data
New contributor
2
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
2 hours ago
2
You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.
– John1024
2 hours ago
@steeldriver Not sure what you mean, but I'm trying to write a Bash script to do a series of tasks, amongst which is this one, so I need a Bash command to do this rather than any other language.
– OhLook
1 hour ago
@John1024 I tried no code 'cos I didn't know which command I should use, and that's why I came here to ask.
– OhLook
1 hour ago
add a comment |
I have a txt file that contains some numbers like this:
1
2
3
4
5
And I have another txt file that contains the same number of lines, but with other numbers:
6
7
8
9
10
I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?
bash shell-script text-processing numeric-data
New contributor
I have a txt file that contains some numbers like this:
1
2
3
4
5
And I have another txt file that contains the same number of lines, but with other numbers:
6
7
8
9
10
I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?
bash shell-script text-processing numeric-data
bash shell-script text-processing numeric-data
New contributor
New contributor
edited 1 hour ago
jimmij
31.6k873108
31.6k873108
New contributor
asked 2 hours ago
OhLookOhLook
1091
1091
New contributor
New contributor
2
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
2 hours ago
2
You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.
– John1024
2 hours ago
@steeldriver Not sure what you mean, but I'm trying to write a Bash script to do a series of tasks, amongst which is this one, so I need a Bash command to do this rather than any other language.
– OhLook
1 hour ago
@John1024 I tried no code 'cos I didn't know which command I should use, and that's why I came here to ask.
– OhLook
1 hour ago
add a comment |
2
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
2 hours ago
2
You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.
– John1024
2 hours ago
@steeldriver Not sure what you mean, but I'm trying to write a Bash script to do a series of tasks, amongst which is this one, so I need a Bash command to do this rather than any other language.
– OhLook
1 hour ago
@John1024 I tried no code 'cos I didn't know which command I should use, and that's why I came here to ask.
– OhLook
1 hour ago
2
2
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
2 hours ago
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
2 hours ago
2
2
You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.
– John1024
2 hours ago
You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.
– John1024
2 hours ago
@steeldriver Not sure what you mean, but I'm trying to write a Bash script to do a series of tasks, amongst which is this one, so I need a Bash command to do this rather than any other language.
– OhLook
1 hour ago
@steeldriver Not sure what you mean, but I'm trying to write a Bash script to do a series of tasks, amongst which is this one, so I need a Bash command to do this rather than any other language.
– OhLook
1 hour ago
@John1024 I tried no code 'cos I didn't know which command I should use, and that's why I came here to ask.
– OhLook
1 hour ago
@John1024 I tried no code 'cos I didn't know which command I should use, and that's why I came here to ask.
– OhLook
1 hour ago
add a comment |
4 Answers
4
active
oldest
votes
This is basic task many tools can solve; paste
+ awk
combo seems exceptionally handy:
$ paste file1 file2 | awk '$0=$1+$2'
7
9
11
13
15
add a comment |
Along the paste
lines, but doing the math with bc
:
$ paste -d+ file1 file2 | bc
7
9
11
13
15
The intermediate result (before bc
):
$ paste -d+ file1 file2
1+6
2+7
3+8
4+9
5+10
For a more bash-centric solution, and assuming that file2 has at least as many lines as file1:
mapfile -t file1 < file1
mapfile -t file2 < file2
for((i=0; i < ${#file1[@]}; i++))
do
printf '%dn' $((file1[i] + file2[i]))
done
Thanks but the paste command doesn't work for me. It says (standard_in) 1: illegal character: ^M. I don't know what it means 'cos there's no ^M in my files. I'll give mapfile a go...
– OhLook
1 hour ago
1
That's an indication that you have a DOS/Windows format file with CRLF line endings instead of just CR. Transfer it differently or re-save it, or post-process it: unix.stackexchange.com/a/192093/117549
– Jeff Schaller
1 hour ago
add a comment |
Ok, it's a little cryptic, but also with bash arithmetic, paste
and sed
.
$ # debug
$ paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2)
echo $((1+6))
echo $((2+7))
echo $((3+8))
echo $((4+9))
echo $((5+10))
$ eval "$(paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2))"
7
9
11
13
15
add a comment |
an awk
-only solution
awk 'getline a <"file2" {print $0 + a}' file1
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
OhLook is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501486%2fhow-do-i-add-numbers-from-two-txt-files-with-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is basic task many tools can solve; paste
+ awk
combo seems exceptionally handy:
$ paste file1 file2 | awk '$0=$1+$2'
7
9
11
13
15
add a comment |
This is basic task many tools can solve; paste
+ awk
combo seems exceptionally handy:
$ paste file1 file2 | awk '$0=$1+$2'
7
9
11
13
15
add a comment |
This is basic task many tools can solve; paste
+ awk
combo seems exceptionally handy:
$ paste file1 file2 | awk '$0=$1+$2'
7
9
11
13
15
This is basic task many tools can solve; paste
+ awk
combo seems exceptionally handy:
$ paste file1 file2 | awk '$0=$1+$2'
7
9
11
13
15
answered 2 hours ago
jimmijjimmij
31.6k873108
31.6k873108
add a comment |
add a comment |
Along the paste
lines, but doing the math with bc
:
$ paste -d+ file1 file2 | bc
7
9
11
13
15
The intermediate result (before bc
):
$ paste -d+ file1 file2
1+6
2+7
3+8
4+9
5+10
For a more bash-centric solution, and assuming that file2 has at least as many lines as file1:
mapfile -t file1 < file1
mapfile -t file2 < file2
for((i=0; i < ${#file1[@]}; i++))
do
printf '%dn' $((file1[i] + file2[i]))
done
Thanks but the paste command doesn't work for me. It says (standard_in) 1: illegal character: ^M. I don't know what it means 'cos there's no ^M in my files. I'll give mapfile a go...
– OhLook
1 hour ago
1
That's an indication that you have a DOS/Windows format file with CRLF line endings instead of just CR. Transfer it differently or re-save it, or post-process it: unix.stackexchange.com/a/192093/117549
– Jeff Schaller
1 hour ago
add a comment |
Along the paste
lines, but doing the math with bc
:
$ paste -d+ file1 file2 | bc
7
9
11
13
15
The intermediate result (before bc
):
$ paste -d+ file1 file2
1+6
2+7
3+8
4+9
5+10
For a more bash-centric solution, and assuming that file2 has at least as many lines as file1:
mapfile -t file1 < file1
mapfile -t file2 < file2
for((i=0; i < ${#file1[@]}; i++))
do
printf '%dn' $((file1[i] + file2[i]))
done
Thanks but the paste command doesn't work for me. It says (standard_in) 1: illegal character: ^M. I don't know what it means 'cos there's no ^M in my files. I'll give mapfile a go...
– OhLook
1 hour ago
1
That's an indication that you have a DOS/Windows format file with CRLF line endings instead of just CR. Transfer it differently or re-save it, or post-process it: unix.stackexchange.com/a/192093/117549
– Jeff Schaller
1 hour ago
add a comment |
Along the paste
lines, but doing the math with bc
:
$ paste -d+ file1 file2 | bc
7
9
11
13
15
The intermediate result (before bc
):
$ paste -d+ file1 file2
1+6
2+7
3+8
4+9
5+10
For a more bash-centric solution, and assuming that file2 has at least as many lines as file1:
mapfile -t file1 < file1
mapfile -t file2 < file2
for((i=0; i < ${#file1[@]}; i++))
do
printf '%dn' $((file1[i] + file2[i]))
done
Along the paste
lines, but doing the math with bc
:
$ paste -d+ file1 file2 | bc
7
9
11
13
15
The intermediate result (before bc
):
$ paste -d+ file1 file2
1+6
2+7
3+8
4+9
5+10
For a more bash-centric solution, and assuming that file2 has at least as many lines as file1:
mapfile -t file1 < file1
mapfile -t file2 < file2
for((i=0; i < ${#file1[@]}; i++))
do
printf '%dn' $((file1[i] + file2[i]))
done
edited 1 hour ago
answered 1 hour ago
Jeff SchallerJeff Schaller
41.6k1056132
41.6k1056132
Thanks but the paste command doesn't work for me. It says (standard_in) 1: illegal character: ^M. I don't know what it means 'cos there's no ^M in my files. I'll give mapfile a go...
– OhLook
1 hour ago
1
That's an indication that you have a DOS/Windows format file with CRLF line endings instead of just CR. Transfer it differently or re-save it, or post-process it: unix.stackexchange.com/a/192093/117549
– Jeff Schaller
1 hour ago
add a comment |
Thanks but the paste command doesn't work for me. It says (standard_in) 1: illegal character: ^M. I don't know what it means 'cos there's no ^M in my files. I'll give mapfile a go...
– OhLook
1 hour ago
1
That's an indication that you have a DOS/Windows format file with CRLF line endings instead of just CR. Transfer it differently or re-save it, or post-process it: unix.stackexchange.com/a/192093/117549
– Jeff Schaller
1 hour ago
Thanks but the paste command doesn't work for me. It says (standard_in) 1: illegal character: ^M. I don't know what it means 'cos there's no ^M in my files. I'll give mapfile a go...
– OhLook
1 hour ago
Thanks but the paste command doesn't work for me. It says (standard_in) 1: illegal character: ^M. I don't know what it means 'cos there's no ^M in my files. I'll give mapfile a go...
– OhLook
1 hour ago
1
1
That's an indication that you have a DOS/Windows format file with CRLF line endings instead of just CR. Transfer it differently or re-save it, or post-process it: unix.stackexchange.com/a/192093/117549
– Jeff Schaller
1 hour ago
That's an indication that you have a DOS/Windows format file with CRLF line endings instead of just CR. Transfer it differently or re-save it, or post-process it: unix.stackexchange.com/a/192093/117549
– Jeff Schaller
1 hour ago
add a comment |
Ok, it's a little cryptic, but also with bash arithmetic, paste
and sed
.
$ # debug
$ paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2)
echo $((1+6))
echo $((2+7))
echo $((3+8))
echo $((4+9))
echo $((5+10))
$ eval "$(paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2))"
7
9
11
13
15
add a comment |
Ok, it's a little cryptic, but also with bash arithmetic, paste
and sed
.
$ # debug
$ paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2)
echo $((1+6))
echo $((2+7))
echo $((3+8))
echo $((4+9))
echo $((5+10))
$ eval "$(paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2))"
7
9
11
13
15
add a comment |
Ok, it's a little cryptic, but also with bash arithmetic, paste
and sed
.
$ # debug
$ paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2)
echo $((1+6))
echo $((2+7))
echo $((3+8))
echo $((4+9))
echo $((5+10))
$ eval "$(paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2))"
7
9
11
13
15
Ok, it's a little cryptic, but also with bash arithmetic, paste
and sed
.
$ # debug
$ paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2)
echo $((1+6))
echo $((2+7))
echo $((3+8))
echo $((4+9))
echo $((5+10))
$ eval "$(paste -d+ <(sed 's/(.*)/echo $((1/' file1) <(sed 's/(.*)/1))/' file2))"
7
9
11
13
15
edited 52 mins ago
answered 1 hour ago
FreddyFreddy
3498
3498
add a comment |
add a comment |
an awk
-only solution
awk 'getline a <"file2" {print $0 + a}' file1
add a comment |
an awk
-only solution
awk 'getline a <"file2" {print $0 + a}' file1
add a comment |
an awk
-only solution
awk 'getline a <"file2" {print $0 + a}' file1
an awk
-only solution
awk 'getline a <"file2" {print $0 + a}' file1
edited 41 mins ago
answered 56 mins ago
iruvariruvar
11.9k62960
11.9k62960
add a comment |
add a comment |
OhLook is a new contributor. Be nice, and check out our Code of Conduct.
OhLook is a new contributor. Be nice, and check out our Code of Conduct.
OhLook is a new contributor. Be nice, and check out our Code of Conduct.
OhLook is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501486%2fhow-do-i-add-numbers-from-two-txt-files-with-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
2 hours ago
2
You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.
– John1024
2 hours ago
@steeldriver Not sure what you mean, but I'm trying to write a Bash script to do a series of tasks, amongst which is this one, so I need a Bash command to do this rather than any other language.
– OhLook
1 hour ago
@John1024 I tried no code 'cos I didn't know which command I should use, and that's why I came here to ask.
– OhLook
1 hour ago