AWK - sum plus and minus values into separate variablesHow to use awk or sed to convert csv diffs into more...
My players want to grind XP but we're using landmark advancement
Is it legal to have an abortion in another state or abroad?
Is there a context where the expression `a.b::c` makes sense?
Should there be an "a" before "ten years imprisonment"?
Why are Stein manifolds/spaces the analog of affine varieties/schemes in algebraic geometry?
Can I tell a prospective employee that everyone in the team is leaving?
What weight should be given to writers groups critiques?
Job Market: should one hide their (young) age?
Why did other houses not demand this?
Dealing with spaghetti codebase, manager asks for things I can't deliver
The art of clickbait captions
If a (distance) metric on a connected Riemannian manifold locally agrees with the Riemannian metric, is it equal to the induced metric?
Why did Jon Snow do this immoral act if he is so honorable?
Why do we need to chain the blocks (creating blockchain) in a permissioned blockchain?
How did NASA Langley end up with the first 737?
How do I superimpose two math symbols?
USPS Back Room - Trespassing?
How do I get the ς (final sigma) symbol?
Natural Armour and Weapons
Can a person survive on blood in place of water?
Shorten or merge multiple lines of `&> /dev/null &`
Why does Bran want to find Drogon?
ESTA validity after a visa denial
Is superuser the same as root?
AWK - sum plus and minus values into separate variables
How to use awk or sed to convert csv diffs into more readable formatMatch first fields of two tab separated files and print matching valuessum pair of columns based on matching fieldsHow to select rows from a CSV file based on different column values?Comapring columns in 2 files and printing the values that differAWK, Sum of categoryMerge two files and plus the second column using AWKawk to sum the numbers(floating) and group it on a unique keyHow to get sum of values in column based on variables in other column separately?How to sum each column and print column name and column sum using awk?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a CSV file like this:
2019.04.15;3.75;
2019.04.29;-5.17;
2019.05.01;7.5;
2019.05.06;0.5;
2019.05.13;0.25;
2019.05.20;-8.5;
I want to get the sum of the plus and the minus values off the second column.
I solved this with the following pipe using awk and grep:
plus=$(awk -F';' '{print $2};' "$file" |
grep --invert-match "-" |
awk '{s+=$1}END{print s}'
)
minus=$(awk -F';' '{print $2};' "$file" |
grep "-" |
awk '{s+=$1}END{print s}'
)
I'm pretty sure awk can do it on his own, by using one command, the question is how would it look like?
text-processing awk csv
add a comment |
I have a CSV file like this:
2019.04.15;3.75;
2019.04.29;-5.17;
2019.05.01;7.5;
2019.05.06;0.5;
2019.05.13;0.25;
2019.05.20;-8.5;
I want to get the sum of the plus and the minus values off the second column.
I solved this with the following pipe using awk and grep:
plus=$(awk -F';' '{print $2};' "$file" |
grep --invert-match "-" |
awk '{s+=$1}END{print s}'
)
minus=$(awk -F';' '{print $2};' "$file" |
grep "-" |
awk '{s+=$1}END{print s}'
)
I'm pretty sure awk can do it on his own, by using one command, the question is how would it look like?
text-processing awk csv
add a comment |
I have a CSV file like this:
2019.04.15;3.75;
2019.04.29;-5.17;
2019.05.01;7.5;
2019.05.06;0.5;
2019.05.13;0.25;
2019.05.20;-8.5;
I want to get the sum of the plus and the minus values off the second column.
I solved this with the following pipe using awk and grep:
plus=$(awk -F';' '{print $2};' "$file" |
grep --invert-match "-" |
awk '{s+=$1}END{print s}'
)
minus=$(awk -F';' '{print $2};' "$file" |
grep "-" |
awk '{s+=$1}END{print s}'
)
I'm pretty sure awk can do it on his own, by using one command, the question is how would it look like?
text-processing awk csv
I have a CSV file like this:
2019.04.15;3.75;
2019.04.29;-5.17;
2019.05.01;7.5;
2019.05.06;0.5;
2019.05.13;0.25;
2019.05.20;-8.5;
I want to get the sum of the plus and the minus values off the second column.
I solved this with the following pipe using awk and grep:
plus=$(awk -F';' '{print $2};' "$file" |
grep --invert-match "-" |
awk '{s+=$1}END{print s}'
)
minus=$(awk -F';' '{print $2};' "$file" |
grep "-" |
awk '{s+=$1}END{print s}'
)
I'm pretty sure awk can do it on his own, by using one command, the question is how would it look like?
text-processing awk csv
text-processing awk csv
asked 1 hour ago
nathnath
1,038832
1,038832
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
plus:
awk -F';' '$2~/^[^-]/{s+=$2} END{print s}' data
minus:
awk -F';' '$2~/^[-]/{s+=$2} END{print s}' data
Awesome, Thanks!
– nath
1 hour ago
add a comment |
$ set -f; IFS=" "
$ set $(perl -F; -lane '$A[$F[1]<0] += $F[1]}{print "@A"' "$file")
$ plus=$1 minus=$2
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
});
}
});
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%2f520539%2fawk-sum-plus-and-minus-values-into-separate-variables%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
plus:
awk -F';' '$2~/^[^-]/{s+=$2} END{print s}' data
minus:
awk -F';' '$2~/^[-]/{s+=$2} END{print s}' data
Awesome, Thanks!
– nath
1 hour ago
add a comment |
plus:
awk -F';' '$2~/^[^-]/{s+=$2} END{print s}' data
minus:
awk -F';' '$2~/^[-]/{s+=$2} END{print s}' data
Awesome, Thanks!
– nath
1 hour ago
add a comment |
plus:
awk -F';' '$2~/^[^-]/{s+=$2} END{print s}' data
minus:
awk -F';' '$2~/^[-]/{s+=$2} END{print s}' data
plus:
awk -F';' '$2~/^[^-]/{s+=$2} END{print s}' data
minus:
awk -F';' '$2~/^[-]/{s+=$2} END{print s}' data
answered 1 hour ago
dedowsdidedowsdi
54915
54915
Awesome, Thanks!
– nath
1 hour ago
add a comment |
Awesome, Thanks!
– nath
1 hour ago
Awesome, Thanks!
– nath
1 hour ago
Awesome, Thanks!
– nath
1 hour ago
add a comment |
$ set -f; IFS=" "
$ set $(perl -F; -lane '$A[$F[1]<0] += $F[1]}{print "@A"' "$file")
$ plus=$1 minus=$2
add a comment |
$ set -f; IFS=" "
$ set $(perl -F; -lane '$A[$F[1]<0] += $F[1]}{print "@A"' "$file")
$ plus=$1 minus=$2
add a comment |
$ set -f; IFS=" "
$ set $(perl -F; -lane '$A[$F[1]<0] += $F[1]}{print "@A"' "$file")
$ plus=$1 minus=$2
$ set -f; IFS=" "
$ set $(perl -F; -lane '$A[$F[1]<0] += $F[1]}{print "@A"' "$file")
$ plus=$1 minus=$2
answered 36 mins ago
Rakesh SharmaRakesh Sharma
462126
462126
add a comment |
add a comment |
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%2f520539%2fawk-sum-plus-and-minus-values-into-separate-variables%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