Subsetting a variable by pattern
I have a delimited file with the following format that I have assigned to a bash variable "foo":
echo $foo
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
I am trying to find a preferably awk-based solution that can split this variable into new text files such that all of the rows with identical values in column #3 are in one file. In a perfect world, the files would be named after the string in column #3 In this case, the files would be "hello.txt and "world.txt" with the following contents:
cat hello.txt
A 1 hello
B 2 hello
C 3 hello
cat world.txt
D 4 world
E 5 world
F 6 world
EDIT:
I should have included some previous attempts for this as mentioned below. I know how one could do this if the patterns were known ahead of time using something like grep, but am not sure how to allow it to accept wildcards. This is what I normally do if I know I will only need to extract by a very specific pattern.
echo "$foo" | grep -w "hello" > hello.txt
echo "$foo" | grep -w "world" > world.txt
awk wildcards
New contributor
add a comment |
I have a delimited file with the following format that I have assigned to a bash variable "foo":
echo $foo
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
I am trying to find a preferably awk-based solution that can split this variable into new text files such that all of the rows with identical values in column #3 are in one file. In a perfect world, the files would be named after the string in column #3 In this case, the files would be "hello.txt and "world.txt" with the following contents:
cat hello.txt
A 1 hello
B 2 hello
C 3 hello
cat world.txt
D 4 world
E 5 world
F 6 world
EDIT:
I should have included some previous attempts for this as mentioned below. I know how one could do this if the patterns were known ahead of time using something like grep, but am not sure how to allow it to accept wildcards. This is what I normally do if I know I will only need to extract by a very specific pattern.
echo "$foo" | grep -w "hello" > hello.txt
echo "$foo" | grep -w "world" > world.txt
awk wildcards
New contributor
You will get a much more friendly reception and much better help here if you show what code you have tried so far, however inadequate, 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
2
Thank you for the advice. I was really quite stuck and couldn't think of any solutions to this, which is why I had no code to show. The only solutions I could come up with required that the pattern be known beforehand, but will post an example as an edit.
– adam
2 hours ago
add a comment |
I have a delimited file with the following format that I have assigned to a bash variable "foo":
echo $foo
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
I am trying to find a preferably awk-based solution that can split this variable into new text files such that all of the rows with identical values in column #3 are in one file. In a perfect world, the files would be named after the string in column #3 In this case, the files would be "hello.txt and "world.txt" with the following contents:
cat hello.txt
A 1 hello
B 2 hello
C 3 hello
cat world.txt
D 4 world
E 5 world
F 6 world
EDIT:
I should have included some previous attempts for this as mentioned below. I know how one could do this if the patterns were known ahead of time using something like grep, but am not sure how to allow it to accept wildcards. This is what I normally do if I know I will only need to extract by a very specific pattern.
echo "$foo" | grep -w "hello" > hello.txt
echo "$foo" | grep -w "world" > world.txt
awk wildcards
New contributor
I have a delimited file with the following format that I have assigned to a bash variable "foo":
echo $foo
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
I am trying to find a preferably awk-based solution that can split this variable into new text files such that all of the rows with identical values in column #3 are in one file. In a perfect world, the files would be named after the string in column #3 In this case, the files would be "hello.txt and "world.txt" with the following contents:
cat hello.txt
A 1 hello
B 2 hello
C 3 hello
cat world.txt
D 4 world
E 5 world
F 6 world
EDIT:
I should have included some previous attempts for this as mentioned below. I know how one could do this if the patterns were known ahead of time using something like grep, but am not sure how to allow it to accept wildcards. This is what I normally do if I know I will only need to extract by a very specific pattern.
echo "$foo" | grep -w "hello" > hello.txt
echo "$foo" | grep -w "world" > world.txt
awk wildcards
awk wildcards
New contributor
New contributor
edited 2 hours ago
adam
New contributor
asked 2 hours ago
adamadam
83
83
New contributor
New contributor
You will get a much more friendly reception and much better help here if you show what code you have tried so far, however inadequate, 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
2
Thank you for the advice. I was really quite stuck and couldn't think of any solutions to this, which is why I had no code to show. The only solutions I could come up with required that the pattern be known beforehand, but will post an example as an edit.
– adam
2 hours ago
add a comment |
You will get a much more friendly reception and much better help here if you show what code you have tried so far, however inadequate, 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
2
Thank you for the advice. I was really quite stuck and couldn't think of any solutions to this, which is why I had no code to show. The only solutions I could come up with required that the pattern be known beforehand, but will post an example as an edit.
– adam
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, however inadequate, 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, however inadequate, 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
2
2
Thank you for the advice. I was really quite stuck and couldn't think of any solutions to this, which is why I had no code to show. The only solutions I could come up with required that the pattern be known beforehand, but will post an example as an edit.
– adam
2 hours ago
Thank you for the advice. I was really quite stuck and couldn't think of any solutions to this, which is why I had no code to show. The only solutions I could come up with required that the pattern be known beforehand, but will post an example as an edit.
– adam
2 hours ago
add a comment |
1 Answer
1
active
oldest
votes
You should be able to use
awk '{print > $3 ".txt"}' <<< "$foo"
Ex.
$ echo "$foo"
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
awk '{print > $3 ".txt"}' <<< "$foo"
$ head {hello,world}.txt
==> hello.txt <==
A 1 hello
B 2 hello
C 3 hello
==> world.txt <==
D 4 world
E 5 world
F 6 world
Thank you! That makes sense and works perfectly.
– adam
2 hours ago
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
});
}
});
adam 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%2f507313%2fsubsetting-a-variable-by-pattern%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 should be able to use
awk '{print > $3 ".txt"}' <<< "$foo"
Ex.
$ echo "$foo"
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
awk '{print > $3 ".txt"}' <<< "$foo"
$ head {hello,world}.txt
==> hello.txt <==
A 1 hello
B 2 hello
C 3 hello
==> world.txt <==
D 4 world
E 5 world
F 6 world
Thank you! That makes sense and works perfectly.
– adam
2 hours ago
add a comment |
You should be able to use
awk '{print > $3 ".txt"}' <<< "$foo"
Ex.
$ echo "$foo"
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
awk '{print > $3 ".txt"}' <<< "$foo"
$ head {hello,world}.txt
==> hello.txt <==
A 1 hello
B 2 hello
C 3 hello
==> world.txt <==
D 4 world
E 5 world
F 6 world
Thank you! That makes sense and works perfectly.
– adam
2 hours ago
add a comment |
You should be able to use
awk '{print > $3 ".txt"}' <<< "$foo"
Ex.
$ echo "$foo"
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
awk '{print > $3 ".txt"}' <<< "$foo"
$ head {hello,world}.txt
==> hello.txt <==
A 1 hello
B 2 hello
C 3 hello
==> world.txt <==
D 4 world
E 5 world
F 6 world
You should be able to use
awk '{print > $3 ".txt"}' <<< "$foo"
Ex.
$ echo "$foo"
A 1 hello
B 2 hello
C 3 hello
D 4 world
E 5 world
F 6 world
awk '{print > $3 ".txt"}' <<< "$foo"
$ head {hello,world}.txt
==> hello.txt <==
A 1 hello
B 2 hello
C 3 hello
==> world.txt <==
D 4 world
E 5 world
F 6 world
answered 2 hours ago
steeldriversteeldriver
37.2k45287
37.2k45287
Thank you! That makes sense and works perfectly.
– adam
2 hours ago
add a comment |
Thank you! That makes sense and works perfectly.
– adam
2 hours ago
Thank you! That makes sense and works perfectly.
– adam
2 hours ago
Thank you! That makes sense and works perfectly.
– adam
2 hours ago
add a comment |
adam is a new contributor. Be nice, and check out our Code of Conduct.
adam is a new contributor. Be nice, and check out our Code of Conduct.
adam is a new contributor. Be nice, and check out our Code of Conduct.
adam 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%2f507313%2fsubsetting-a-variable-by-pattern%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
You will get a much more friendly reception and much better help here if you show what code you have tried so far, however inadequate, 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
2
Thank you for the advice. I was really quite stuck and couldn't think of any solutions to this, which is why I had no code to show. The only solutions I could come up with required that the pattern be known beforehand, but will post an example as an edit.
– adam
2 hours ago