LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2018, 12:11 AM   #1
TheGeniusLOL
LQ Newbie
 
Registered: Oct 2018
Posts: 22

Rep: Reputation: Disabled
How can I random words from a single line


From

The quick brown fox

to

quick fox The brown

I tried
Code:
echo "The quick brown fox" | rev
xof nworb kciuq ehT

echo "The quick brown fox" | sort -R
The quick brown fox
Does not work. I need your expert suggestions or ideas to try.

Thanks
 
Old 11-02-2018, 12:34 AM   #2
Mike25
Member
 
Registered: Feb 2003
Location: ON, Canada
Distribution: Fedora & RH3
Posts: 56
Blog Entries: 2

Rep: Reputation: 11
Getting late here. I'm too tired to do the code and shutting down for the night in a minute, but what you could do is split the sentence into an array of words, then print the array in reverse.
 
Old 11-02-2018, 12:43 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
'sort' works on lines and you only pass one line to. One line cannot be sorted randomly. Try passing lines to it:

Code:
v="one two three four"
sort -R <(echo -e "${v// /\\n}")
 
1 members found this post helpful.
Old 11-02-2018, 01:14 AM   #4
TheGeniusLOL
LQ Newbie
 
Registered: Oct 2018
Posts: 22

Original Poster
Rep: Reputation: Disabled
Hi crts

I just tried your code
Code:
v="one two three four"
sort -R <(echo -e "${v// /\\n}") 
four
one
two
three
However, it puts each word on it's own line. To get a single line I added the fmt command at the end.

Code:
sort -R <(echo -e "${v// /\\n}") | fmt
one three two four
Thanks crts, you're awesome
 
Old 11-02-2018, 01:43 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by TheGeniusLOL View Post
Hi crts

I just tried your code
Code:
v="one two three four"
sort -R <(echo -e "${v// /\\n}") 
four
one
two
three
However, it puts each word on it's own line. To get a single line I added the fmt command at the end.

Code:
sort -R <(echo -e "${v// /\\n}") | fmt
one three two four
Thanks crts, you're awesome
Nice catch. I just noticed that you might also use 'shuf' for this task:
Code:
shuf -e one two three four
You could also have two words treated as one field:
Code:
shuf -e one "two three" four
This, however, will not be obvious if you need all output in one line and thus pipe it two 'fmt' afterwards.

Last edited by crts; 11-02-2018 at 01:44 AM.
 
2 members found this post helpful.
Old 11-02-2018, 05:42 AM   #6
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by crts View Post
Nice catch. I just noticed that you might also use 'shuf' for this task:
Code:
shuf -e one two three four
You could also have two words treated as one field:
Code:
shuf -e one "two three" four
Well done with shuf -e
Quote:
Originally Posted by crts View Post
This, however, will not be obvious if you need all output in one line and thus pipe it two 'fmt' afterwards.
...or a simple pipe to: tr '\n' ' '
 
Old 11-02-2018, 05:57 PM   #7
TheGeniusLOL
LQ Newbie
 
Registered: Oct 2018
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by crts View Post
Nice catch. I just noticed that you might also use 'shuf' for this task:
Code:
shuf -e one two three four
You could also have two words treated as one field:
Code:
shuf -e one "two three" four
This, however, will not be obvious if you need all output in one line and thus pipe it two 'fmt' afterwards.
I didn't know shuf worked with words. Thanks crts
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] GNU Nano 3.0 - backspace removes whole words instead of single characters? coralfang Linux - Software 5 09-22-2018 01:10 AM
Replacing multiple words with single word using SED Sayantan1986 Linux - Newbie 5 08-02-2017 07:10 AM
[SOLVED] Grep words made up of a single dot Fiatuno Linux - Newbie 4 05-03-2015 02:48 AM
looking for a program to display random words from list newbiesforever Linux - Software 10 04-23-2011 01:21 PM
grep multiple words any order (AND, not OR) single line, from many files cedardoc Linux - Newbie 7 07-29-2010 10:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration