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
1
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
22 mins ago
1
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
15 mins 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 5 mins ago
jimmij
31.6k873108
31.6k873108
New contributor
asked 26 mins ago
OhLookOhLook
1061
1061
New contributor
New contributor
1
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
22 mins ago
1
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
15 mins ago
add a comment |
1
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
22 mins ago
1
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
15 mins ago
1
1
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
22 mins ago
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
22 mins ago
1
1
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
15 mins 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
15 mins ago
add a comment |
2 Answers
2
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
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
2 Answers
2
active
oldest
votes
2 Answers
2
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 7 mins 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
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
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
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
answered 4 mins ago
Jeff SchallerJeff Schaller
41.5k1056132
41.5k1056132
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
1
Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?
– steeldriver
22 mins ago
1
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
15 mins ago