Two-file processing in awk
I have file :
result.txt
Apple fruits 10 20 30
Car vehicle 40 50 60
Book study 70 80 90
Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max
. I have another file config.txt which contains a property for each feature i.e.
config.txt
fruits Max
vehicle Median
study Min
So I want to write a script which shows only that column number for the feature which is defined in config.txt
file.
Expected output:
Apple fruits 30
Car vehicle 50
Book study 70
I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:
awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt
I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.
bash scripts awk
add a comment |
I have file :
result.txt
Apple fruits 10 20 30
Car vehicle 40 50 60
Book study 70 80 90
Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max
. I have another file config.txt which contains a property for each feature i.e.
config.txt
fruits Max
vehicle Median
study Min
So I want to write a script which shows only that column number for the feature which is defined in config.txt
file.
Expected output:
Apple fruits 30
Car vehicle 50
Book study 70
I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:
awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt
I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.
bash scripts awk
add a comment |
I have file :
result.txt
Apple fruits 10 20 30
Car vehicle 40 50 60
Book study 70 80 90
Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max
. I have another file config.txt which contains a property for each feature i.e.
config.txt
fruits Max
vehicle Median
study Min
So I want to write a script which shows only that column number for the feature which is defined in config.txt
file.
Expected output:
Apple fruits 30
Car vehicle 50
Book study 70
I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:
awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt
I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.
bash scripts awk
I have file :
result.txt
Apple fruits 10 20 30
Car vehicle 40 50 60
Book study 70 80 90
Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max
. I have another file config.txt which contains a property for each feature i.e.
config.txt
fruits Max
vehicle Median
study Min
So I want to write a script which shows only that column number for the feature which is defined in config.txt
file.
Expected output:
Apple fruits 30
Car vehicle 50
Book study 70
I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:
awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt
I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.
bash scripts awk
bash scripts awk
asked Feb 19 at 11:37
AryaArya
184
184
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to map the feature names in the config.txt
file to field indices in the result.txt
file, e.g.
awk '
BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
NR==FNR {arr[$1] = ind[$2]; next}
$2 in arr {print $1,$2,$(arr[$2])}
' config.txt result.txt
Apple fruits 30
Car vehicle 50
Book study 70
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1119500%2ftwo-file-processing-in-awk%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
You need to map the feature names in the config.txt
file to field indices in the result.txt
file, e.g.
awk '
BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
NR==FNR {arr[$1] = ind[$2]; next}
$2 in arr {print $1,$2,$(arr[$2])}
' config.txt result.txt
Apple fruits 30
Car vehicle 50
Book study 70
add a comment |
You need to map the feature names in the config.txt
file to field indices in the result.txt
file, e.g.
awk '
BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
NR==FNR {arr[$1] = ind[$2]; next}
$2 in arr {print $1,$2,$(arr[$2])}
' config.txt result.txt
Apple fruits 30
Car vehicle 50
Book study 70
add a comment |
You need to map the feature names in the config.txt
file to field indices in the result.txt
file, e.g.
awk '
BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
NR==FNR {arr[$1] = ind[$2]; next}
$2 in arr {print $1,$2,$(arr[$2])}
' config.txt result.txt
Apple fruits 30
Car vehicle 50
Book study 70
You need to map the feature names in the config.txt
file to field indices in the result.txt
file, e.g.
awk '
BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
NR==FNR {arr[$1] = ind[$2]; next}
$2 in arr {print $1,$2,$(arr[$2])}
' config.txt result.txt
Apple fruits 30
Car vehicle 50
Book study 70
answered Feb 19 at 13:06
steeldriversteeldriver
69.6k11114186
69.6k11114186
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1119500%2ftwo-file-processing-in-awk%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