Shuffle words in a stringString index processingForce newlines with cat wildcard printingAppending a...
What are the map units that WGS84 uses?
Does the Giant Toad's Swallow acid damage take effect only at the start of its next turn?
Opportunity profits vs. opportunity costs
Looking for a big fantasy novel about scholarly monks that sort of worship math?
Why Is Sojdlg123aljg a Common Password?
How to create large inductors (1H) for audio use?
extract specific cheracters from each line
Looking for the comic book where Spider-Man was [mistakenly] addressed as Super-Man
Why are UK MPs allowed to not vote (but it counts as a no)?
What is the purpose of the rotating plate in front of the lock?
Temporarily simulate being offline programmatically
My Friend James
How do I write a vertically-stacked definition of a sequence?
Why does 8 bit truecolor use only 2 bits for blue?
Male viewpoint in an erotic novel
Can you fix a tube with a lighter?
What do English-speaking kids call ice-cream on a stick?
If I have an accident, should I file a claim with my car insurance company?
How to calculate the power level of a Commander deck?
Translate English to Pig Latin | PIG_LATIN.PY
Is it right to use the ideas of non-winning designers in a design contest?
What quests do you need to stop at before you make an enemy of a faction for each faction?
How should Thaumaturgy's "three times as loud as normal" be interpreted?
What is the justification for Dirac's large numbers hypothesis?
Shuffle words in a string
String index processingForce newlines with cat wildcard printingAppending a character in the nth position of a matching stringbash script that incorporates content from a file as part of a commandHow to benchmark different text processing commands and find out the fastest?change ods to txt, with all columns from ods correctly distributed with tab delimiterBatch sorting multiple files and removing duplicate lines from multiple files - in place if possibleCSV File Date FormattingMulti-line file shuffleMulti-line String Pattern Matching, Insertion and Deletion with sed or awk
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a text file with newline delimited strings. My problem is to process each line as follows: shuffle the order of tokens by using space as a delimiter.
For example:
Input:
A B C
Output:
C A B
Running the command/script repeatedly should of course provide different order.
My current solution (for a single text line):
$ cat <file> | tr " " "n" | shuf | tr "n" " "
Is there a nice (a better) command line combo to process a text file with multiple lines?
linux bash text-processing
add a comment |
I have a text file with newline delimited strings. My problem is to process each line as follows: shuffle the order of tokens by using space as a delimiter.
For example:
Input:
A B C
Output:
C A B
Running the command/script repeatedly should of course provide different order.
My current solution (for a single text line):
$ cat <file> | tr " " "n" | shuf | tr "n" " "
Is there a nice (a better) command line combo to process a text file with multiple lines?
linux bash text-processing
1
don't abusecat,tr " " "n" < <file> | shuf | tr "n" " "is the same.
– Quora Feans
49 mins ago
add a comment |
I have a text file with newline delimited strings. My problem is to process each line as follows: shuffle the order of tokens by using space as a delimiter.
For example:
Input:
A B C
Output:
C A B
Running the command/script repeatedly should of course provide different order.
My current solution (for a single text line):
$ cat <file> | tr " " "n" | shuf | tr "n" " "
Is there a nice (a better) command line combo to process a text file with multiple lines?
linux bash text-processing
I have a text file with newline delimited strings. My problem is to process each line as follows: shuffle the order of tokens by using space as a delimiter.
For example:
Input:
A B C
Output:
C A B
Running the command/script repeatedly should of course provide different order.
My current solution (for a single text line):
$ cat <file> | tr " " "n" | shuf | tr "n" " "
Is there a nice (a better) command line combo to process a text file with multiple lines?
linux bash text-processing
linux bash text-processing
asked 1 hour ago
Vladislavs DovgalecsVladislavs Dovgalecs
4193 silver badges10 bronze badges
4193 silver badges10 bronze badges
1
don't abusecat,tr " " "n" < <file> | shuf | tr "n" " "is the same.
– Quora Feans
49 mins ago
add a comment |
1
don't abusecat,tr " " "n" < <file> | shuf | tr "n" " "is the same.
– Quora Feans
49 mins ago
1
1
don't abuse
cat, tr " " "n" < <file> | shuf | tr "n" " " is the same.– Quora Feans
49 mins ago
don't abuse
cat, tr " " "n" < <file> | shuf | tr "n" " " is the same.– Quora Feans
49 mins ago
add a comment |
3 Answers
3
active
oldest
votes
To pass the parameters as one line:
shuf -e one two three four is what you need.
shuf -e $(cat <file>) | tr "n" " " for a file with one line, as in your example.
For multiple lines:
while read line; do shuf -e $line | tr "n" " " && echo n; done < <file>
This will print each element out on a newline though..
– Zhenhir
32 mins ago
@Zhenhir: edited.
– Quora Feans
28 mins ago
That’s funny; just a moment ago I read a comment that said “don’t abusecat”.
– G-Man
3 mins ago
add a comment |
Given
$ cat file
A B C
D E F
G H I J
then using shuffle from perl's List::Util module:
$ perl -MList::Util=shuffle -alpe '$_ = join " ", shuffle @F' file
C B A
E D F
I J G H
With bash read -a and shuf (it's a little tricky to get the delimiters right):
$ while read -a arr; do shuf -e "${arr[@]}" | awk -vRS= -F'n' '{NF+=0} 1'; done < file
A C B
F E D
J I G H
add a comment |
Your original command can be simplified to
shuf -e A B C | tr "n" " " && echo ""
or
shuffled=( $(shuf -e A B C) ) ; echo ${shuffled[*]}
Which I think is a little less hacky and is also faster from my rudimentary tests.
If you have a file at ~/test which contains
A B C
D E F
You can shuffle and echo each line with the following command
while read line; do shuffled=( $(shuf -e $line) ) ; echo ${shuffled[*]} ; done < ~/test
result:
B C A
G E F
How this works:
shuf -e splits on spaces as well as newlines.. but only because it will treat A B C as three arguments.
so
shuf -e A B C
will shuffle A B and C
but shuf -e "A B C"
will not shuffle A B and C
We can use this to read each line into an array and then print it out again with echo.
while read line;
Reads in each line into $line when it is passed with < to this loop.
do shuffled=( $(shuf -e $line) )
Makes an array out of each line in the $shuffled variable, by literally expanding shuf -e $line to shuf -e A B C.
echo ${shuffled[*]}
echos our array, by default printing each element with spaces in between
< ~/test
feeds lines from ~/test into our loop.
New contributor
Zhenhir 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 |
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/4.0/"u003ecc by-sa 4.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%2f539279%2fshuffle-words-in-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
To pass the parameters as one line:
shuf -e one two three four is what you need.
shuf -e $(cat <file>) | tr "n" " " for a file with one line, as in your example.
For multiple lines:
while read line; do shuf -e $line | tr "n" " " && echo n; done < <file>
This will print each element out on a newline though..
– Zhenhir
32 mins ago
@Zhenhir: edited.
– Quora Feans
28 mins ago
That’s funny; just a moment ago I read a comment that said “don’t abusecat”.
– G-Man
3 mins ago
add a comment |
To pass the parameters as one line:
shuf -e one two three four is what you need.
shuf -e $(cat <file>) | tr "n" " " for a file with one line, as in your example.
For multiple lines:
while read line; do shuf -e $line | tr "n" " " && echo n; done < <file>
This will print each element out on a newline though..
– Zhenhir
32 mins ago
@Zhenhir: edited.
– Quora Feans
28 mins ago
That’s funny; just a moment ago I read a comment that said “don’t abusecat”.
– G-Man
3 mins ago
add a comment |
To pass the parameters as one line:
shuf -e one two three four is what you need.
shuf -e $(cat <file>) | tr "n" " " for a file with one line, as in your example.
For multiple lines:
while read line; do shuf -e $line | tr "n" " " && echo n; done < <file>
To pass the parameters as one line:
shuf -e one two three four is what you need.
shuf -e $(cat <file>) | tr "n" " " for a file with one line, as in your example.
For multiple lines:
while read line; do shuf -e $line | tr "n" " " && echo n; done < <file>
edited 30 mins ago
answered 39 mins ago
Quora FeansQuora Feans
1,5775 gold badges21 silver badges37 bronze badges
1,5775 gold badges21 silver badges37 bronze badges
This will print each element out on a newline though..
– Zhenhir
32 mins ago
@Zhenhir: edited.
– Quora Feans
28 mins ago
That’s funny; just a moment ago I read a comment that said “don’t abusecat”.
– G-Man
3 mins ago
add a comment |
This will print each element out on a newline though..
– Zhenhir
32 mins ago
@Zhenhir: edited.
– Quora Feans
28 mins ago
That’s funny; just a moment ago I read a comment that said “don’t abusecat”.
– G-Man
3 mins ago
This will print each element out on a newline though..
– Zhenhir
32 mins ago
This will print each element out on a newline though..
– Zhenhir
32 mins ago
@Zhenhir: edited.
– Quora Feans
28 mins ago
@Zhenhir: edited.
– Quora Feans
28 mins ago
That’s funny; just a moment ago I read a comment that said “don’t abuse
cat”.– G-Man
3 mins ago
That’s funny; just a moment ago I read a comment that said “don’t abuse
cat”.– G-Man
3 mins ago
add a comment |
Given
$ cat file
A B C
D E F
G H I J
then using shuffle from perl's List::Util module:
$ perl -MList::Util=shuffle -alpe '$_ = join " ", shuffle @F' file
C B A
E D F
I J G H
With bash read -a and shuf (it's a little tricky to get the delimiters right):
$ while read -a arr; do shuf -e "${arr[@]}" | awk -vRS= -F'n' '{NF+=0} 1'; done < file
A C B
F E D
J I G H
add a comment |
Given
$ cat file
A B C
D E F
G H I J
then using shuffle from perl's List::Util module:
$ perl -MList::Util=shuffle -alpe '$_ = join " ", shuffle @F' file
C B A
E D F
I J G H
With bash read -a and shuf (it's a little tricky to get the delimiters right):
$ while read -a arr; do shuf -e "${arr[@]}" | awk -vRS= -F'n' '{NF+=0} 1'; done < file
A C B
F E D
J I G H
add a comment |
Given
$ cat file
A B C
D E F
G H I J
then using shuffle from perl's List::Util module:
$ perl -MList::Util=shuffle -alpe '$_ = join " ", shuffle @F' file
C B A
E D F
I J G H
With bash read -a and shuf (it's a little tricky to get the delimiters right):
$ while read -a arr; do shuf -e "${arr[@]}" | awk -vRS= -F'n' '{NF+=0} 1'; done < file
A C B
F E D
J I G H
Given
$ cat file
A B C
D E F
G H I J
then using shuffle from perl's List::Util module:
$ perl -MList::Util=shuffle -alpe '$_ = join " ", shuffle @F' file
C B A
E D F
I J G H
With bash read -a and shuf (it's a little tricky to get the delimiters right):
$ while read -a arr; do shuf -e "${arr[@]}" | awk -vRS= -F'n' '{NF+=0} 1'; done < file
A C B
F E D
J I G H
answered 29 mins ago
steeldriversteeldriver
43k5 gold badges56 silver badges95 bronze badges
43k5 gold badges56 silver badges95 bronze badges
add a comment |
add a comment |
Your original command can be simplified to
shuf -e A B C | tr "n" " " && echo ""
or
shuffled=( $(shuf -e A B C) ) ; echo ${shuffled[*]}
Which I think is a little less hacky and is also faster from my rudimentary tests.
If you have a file at ~/test which contains
A B C
D E F
You can shuffle and echo each line with the following command
while read line; do shuffled=( $(shuf -e $line) ) ; echo ${shuffled[*]} ; done < ~/test
result:
B C A
G E F
How this works:
shuf -e splits on spaces as well as newlines.. but only because it will treat A B C as three arguments.
so
shuf -e A B C
will shuffle A B and C
but shuf -e "A B C"
will not shuffle A B and C
We can use this to read each line into an array and then print it out again with echo.
while read line;
Reads in each line into $line when it is passed with < to this loop.
do shuffled=( $(shuf -e $line) )
Makes an array out of each line in the $shuffled variable, by literally expanding shuf -e $line to shuf -e A B C.
echo ${shuffled[*]}
echos our array, by default printing each element with spaces in between
< ~/test
feeds lines from ~/test into our loop.
New contributor
Zhenhir 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 |
Your original command can be simplified to
shuf -e A B C | tr "n" " " && echo ""
or
shuffled=( $(shuf -e A B C) ) ; echo ${shuffled[*]}
Which I think is a little less hacky and is also faster from my rudimentary tests.
If you have a file at ~/test which contains
A B C
D E F
You can shuffle and echo each line with the following command
while read line; do shuffled=( $(shuf -e $line) ) ; echo ${shuffled[*]} ; done < ~/test
result:
B C A
G E F
How this works:
shuf -e splits on spaces as well as newlines.. but only because it will treat A B C as three arguments.
so
shuf -e A B C
will shuffle A B and C
but shuf -e "A B C"
will not shuffle A B and C
We can use this to read each line into an array and then print it out again with echo.
while read line;
Reads in each line into $line when it is passed with < to this loop.
do shuffled=( $(shuf -e $line) )
Makes an array out of each line in the $shuffled variable, by literally expanding shuf -e $line to shuf -e A B C.
echo ${shuffled[*]}
echos our array, by default printing each element with spaces in between
< ~/test
feeds lines from ~/test into our loop.
New contributor
Zhenhir 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 |
Your original command can be simplified to
shuf -e A B C | tr "n" " " && echo ""
or
shuffled=( $(shuf -e A B C) ) ; echo ${shuffled[*]}
Which I think is a little less hacky and is also faster from my rudimentary tests.
If you have a file at ~/test which contains
A B C
D E F
You can shuffle and echo each line with the following command
while read line; do shuffled=( $(shuf -e $line) ) ; echo ${shuffled[*]} ; done < ~/test
result:
B C A
G E F
How this works:
shuf -e splits on spaces as well as newlines.. but only because it will treat A B C as three arguments.
so
shuf -e A B C
will shuffle A B and C
but shuf -e "A B C"
will not shuffle A B and C
We can use this to read each line into an array and then print it out again with echo.
while read line;
Reads in each line into $line when it is passed with < to this loop.
do shuffled=( $(shuf -e $line) )
Makes an array out of each line in the $shuffled variable, by literally expanding shuf -e $line to shuf -e A B C.
echo ${shuffled[*]}
echos our array, by default printing each element with spaces in between
< ~/test
feeds lines from ~/test into our loop.
New contributor
Zhenhir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Your original command can be simplified to
shuf -e A B C | tr "n" " " && echo ""
or
shuffled=( $(shuf -e A B C) ) ; echo ${shuffled[*]}
Which I think is a little less hacky and is also faster from my rudimentary tests.
If you have a file at ~/test which contains
A B C
D E F
You can shuffle and echo each line with the following command
while read line; do shuffled=( $(shuf -e $line) ) ; echo ${shuffled[*]} ; done < ~/test
result:
B C A
G E F
How this works:
shuf -e splits on spaces as well as newlines.. but only because it will treat A B C as three arguments.
so
shuf -e A B C
will shuffle A B and C
but shuf -e "A B C"
will not shuffle A B and C
We can use this to read each line into an array and then print it out again with echo.
while read line;
Reads in each line into $line when it is passed with < to this loop.
do shuffled=( $(shuf -e $line) )
Makes an array out of each line in the $shuffled variable, by literally expanding shuf -e $line to shuf -e A B C.
echo ${shuffled[*]}
echos our array, by default printing each element with spaces in between
< ~/test
feeds lines from ~/test into our loop.
New contributor
Zhenhir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 mins ago
New contributor
Zhenhir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 40 mins ago
ZhenhirZhenhir
1092 bronze badges
1092 bronze badges
New contributor
Zhenhir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Zhenhir 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 |
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%2f539279%2fshuffle-words-in-a-string%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
1
don't abuse
cat,tr " " "n" < <file> | shuf | tr "n" " "is the same.– Quora Feans
49 mins ago