Subsetting a variable by pattern2019 Community Moderator ElectionHow to transform two delimited ASCII filesCompare 2 delimited files and output differencesbash: how to add assigned value to fileSubstitute Column With Result of CommandBasic grep/awk help - extracting all lines containing a list of terms from one file into a separate fileBash pattern to match directories whose names begin with a dot (period), by being “explicit”, instead of using “shopt -s dotglob”?How can I match the date after hitting a pattern match in awk to assure the match is current?extract word from file using information from a subset of the file (multiple steps)Print value of 2nd pattern if 1st pattern matches usernameReplace block of text with sed, awk, perl or vim
How to test the sharpness of a knife?
Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?
How much do grades matter for a future academia position?
How to leave product feedback on macOS?
Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?
Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?
Animation: customize bounce interpolation
ContourPlot — How do I color by contour curvature?
When and why was runway 07/25 at Kai Tak removed?
Quoting Keynes in a lecture
Giving feedback to someone without sounding prejudiced
What happens if I try to grapple mirror image?
Given this phrasing in the lease, when should I pay my rent?
SOQL query causes internal Salesforce error
Cumulative Sum using Java 8 stream API
How do I fix the group tension caused by my character stealing and possibly killing without provocation?
Why is the sun approximated as a black body at ~ 5800 K?
El Dorado Word Puzzle II: Videogame Edition
Isometric embedding of a genus g surface
Sound waves in different octaves
Proving an identity involving cross products and coplanar vectors
Why does the Persian emissary display a string of crowned skulls?
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
Showing mass murder in a kid's book
Subsetting a variable by pattern
2019 Community Moderator ElectionHow to transform two delimited ASCII filesCompare 2 delimited files and output differencesbash: how to add assigned value to fileSubstitute Column With Result of CommandBasic grep/awk help - extracting all lines containing a list of terms from one file into a separate fileBash pattern to match directories whose names begin with a dot (period), by being “explicit”, instead of using “shopt -s dotglob”?How can I match the date after hitting a pattern match in awk to assure the match is current?extract word from file using information from a subset of the file (multiple steps)Print value of 2nd pattern if 1st pattern matches usernameReplace block of text with sed, awk, perl or vim
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
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
22 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
22 hours ago
Excellent update.
– John1024
1 hour 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
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 22 hours ago
adam
New contributor
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 22 hours ago
adamadam
83
83
New contributor
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
22 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
22 hours ago
Excellent update.
– John1024
1 hour 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
22 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
22 hours ago
Excellent update.
– John1024
1 hour 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
22 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
22 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
22 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
22 hours ago
Excellent update.
– John1024
1 hour ago
Excellent update.
– John1024
1 hour 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
22 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
22 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
22 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 22 hours ago
steeldriversteeldriver
37.2k45287
37.2k45287
Thank you! That makes sense and works perfectly.
– adam
22 hours ago
add a comment |
Thank you! That makes sense and works perfectly.
– adam
22 hours ago
Thank you! That makes sense and works perfectly.
– adam
22 hours ago
Thank you! That makes sense and works perfectly.
– adam
22 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
22 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
22 hours ago
Excellent update.
– John1024
1 hour ago