compare column of two files and print data accordingly
up vote
0
down vote
favorite
I would like to compare the 2nd column of file1- freememory with the 2nd column-usedmemory of file2. If file1(2nd column) that is "freememory" > file2 (2nd column) that is "usedmemory" then print file2(1st column)-"machine" can be relocated to file1(1st column)-"storage", else file2(1st column)-"machine" CANNOT be relocated to file1(1st column)-"storage",
The comparison should be between 1st line of file2 and 1st line of file1. 2nd line of file2 and 2nd line of file1. Which means nth line of file2 should be compared with nth line of file1 only.
Both the files are sorted in descending order.
file1 is sorted based on descending order of column2
file2 is sorted based on descending order of column2.
file1-
Storage,Freememory
0843,282856
0867,270891
0842,232803
0868,213426
0849,188785
0844,188784
0860,169249
0855,169246
0862,169245
0853,169244
0850,112497
0857,112496
0841,112496
0839,112495
0848,112494
0851,112493
file2 -
Machine,UsedMemory
x0aaa06,111113232
x0aaa05,78851
x0aaa01,10243
x0aaa03,4099
Desired output -
x0aaa06 cannot be relocated to 0843
x0aaa05 can be relocated to 0867
x0aaa01 can be relocated to 0842
x0aaa03 can be relocated to 0868
awk for
add a comment |
up vote
0
down vote
favorite
I would like to compare the 2nd column of file1- freememory with the 2nd column-usedmemory of file2. If file1(2nd column) that is "freememory" > file2 (2nd column) that is "usedmemory" then print file2(1st column)-"machine" can be relocated to file1(1st column)-"storage", else file2(1st column)-"machine" CANNOT be relocated to file1(1st column)-"storage",
The comparison should be between 1st line of file2 and 1st line of file1. 2nd line of file2 and 2nd line of file1. Which means nth line of file2 should be compared with nth line of file1 only.
Both the files are sorted in descending order.
file1 is sorted based on descending order of column2
file2 is sorted based on descending order of column2.
file1-
Storage,Freememory
0843,282856
0867,270891
0842,232803
0868,213426
0849,188785
0844,188784
0860,169249
0855,169246
0862,169245
0853,169244
0850,112497
0857,112496
0841,112496
0839,112495
0848,112494
0851,112493
file2 -
Machine,UsedMemory
x0aaa06,111113232
x0aaa05,78851
x0aaa01,10243
x0aaa03,4099
Desired output -
x0aaa06 cannot be relocated to 0843
x0aaa05 can be relocated to 0867
x0aaa01 can be relocated to 0842
x0aaa03 can be relocated to 0868
awk for
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to compare the 2nd column of file1- freememory with the 2nd column-usedmemory of file2. If file1(2nd column) that is "freememory" > file2 (2nd column) that is "usedmemory" then print file2(1st column)-"machine" can be relocated to file1(1st column)-"storage", else file2(1st column)-"machine" CANNOT be relocated to file1(1st column)-"storage",
The comparison should be between 1st line of file2 and 1st line of file1. 2nd line of file2 and 2nd line of file1. Which means nth line of file2 should be compared with nth line of file1 only.
Both the files are sorted in descending order.
file1 is sorted based on descending order of column2
file2 is sorted based on descending order of column2.
file1-
Storage,Freememory
0843,282856
0867,270891
0842,232803
0868,213426
0849,188785
0844,188784
0860,169249
0855,169246
0862,169245
0853,169244
0850,112497
0857,112496
0841,112496
0839,112495
0848,112494
0851,112493
file2 -
Machine,UsedMemory
x0aaa06,111113232
x0aaa05,78851
x0aaa01,10243
x0aaa03,4099
Desired output -
x0aaa06 cannot be relocated to 0843
x0aaa05 can be relocated to 0867
x0aaa01 can be relocated to 0842
x0aaa03 can be relocated to 0868
awk for
I would like to compare the 2nd column of file1- freememory with the 2nd column-usedmemory of file2. If file1(2nd column) that is "freememory" > file2 (2nd column) that is "usedmemory" then print file2(1st column)-"machine" can be relocated to file1(1st column)-"storage", else file2(1st column)-"machine" CANNOT be relocated to file1(1st column)-"storage",
The comparison should be between 1st line of file2 and 1st line of file1. 2nd line of file2 and 2nd line of file1. Which means nth line of file2 should be compared with nth line of file1 only.
Both the files are sorted in descending order.
file1 is sorted based on descending order of column2
file2 is sorted based on descending order of column2.
file1-
Storage,Freememory
0843,282856
0867,270891
0842,232803
0868,213426
0849,188785
0844,188784
0860,169249
0855,169246
0862,169245
0853,169244
0850,112497
0857,112496
0841,112496
0839,112495
0848,112494
0851,112493
file2 -
Machine,UsedMemory
x0aaa06,111113232
x0aaa05,78851
x0aaa01,10243
x0aaa03,4099
Desired output -
x0aaa06 cannot be relocated to 0843
x0aaa05 can be relocated to 0867
x0aaa01 can be relocated to 0842
x0aaa03 can be relocated to 0868
awk for
awk for
edited yesterday
GAD3R
25.1k1749106
25.1k1749106
asked yesterday
neha dhawan
4117
4117
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I'll assume file1 doesn't have an empty 2nd line.
paste -d, file1 file2 | awk -F, 'NR>1{if ($2 > $4) print $3,"can be relocated to",$1 ; else print $3,"cannot be relocated to",$1}'
Using paste
to feed awk a single "file" consisting of the combined columns of the respective lines.
The awk itself is pretty straightforward, starting at line 2 (NR > 1) and using print instead of printf because I'm lazy.
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',
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%2f489200%2fcompare-column-of-two-files-and-print-data-accordingly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I'll assume file1 doesn't have an empty 2nd line.
paste -d, file1 file2 | awk -F, 'NR>1{if ($2 > $4) print $3,"can be relocated to",$1 ; else print $3,"cannot be relocated to",$1}'
Using paste
to feed awk a single "file" consisting of the combined columns of the respective lines.
The awk itself is pretty straightforward, starting at line 2 (NR > 1) and using print instead of printf because I'm lazy.
add a comment |
up vote
1
down vote
accepted
I'll assume file1 doesn't have an empty 2nd line.
paste -d, file1 file2 | awk -F, 'NR>1{if ($2 > $4) print $3,"can be relocated to",$1 ; else print $3,"cannot be relocated to",$1}'
Using paste
to feed awk a single "file" consisting of the combined columns of the respective lines.
The awk itself is pretty straightforward, starting at line 2 (NR > 1) and using print instead of printf because I'm lazy.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I'll assume file1 doesn't have an empty 2nd line.
paste -d, file1 file2 | awk -F, 'NR>1{if ($2 > $4) print $3,"can be relocated to",$1 ; else print $3,"cannot be relocated to",$1}'
Using paste
to feed awk a single "file" consisting of the combined columns of the respective lines.
The awk itself is pretty straightforward, starting at line 2 (NR > 1) and using print instead of printf because I'm lazy.
I'll assume file1 doesn't have an empty 2nd line.
paste -d, file1 file2 | awk -F, 'NR>1{if ($2 > $4) print $3,"can be relocated to",$1 ; else print $3,"cannot be relocated to",$1}'
Using paste
to feed awk a single "file" consisting of the combined columns of the respective lines.
The awk itself is pretty straightforward, starting at line 2 (NR > 1) and using print instead of printf because I'm lazy.
edited yesterday
answered yesterday
Dani_l
3,115929
3,115929
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f489200%2fcompare-column-of-two-files-and-print-data-accordingly%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