sort words in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort words in a line
# 1  
Old 06-17-2009
sort words in a line

Hi
Im looking for a way, hopefully a one-liner to sort words in a line

e.g

"these are the words in a line"
to
"a are in line the these words"

Thanks!
# 2  
Old 06-17-2009
Quote:
Originally Posted by rebelbuttmunch
Hi
Im looking for a way, hopefully a one-liner to sort words in a line

e.g

"these are the words in a line"
to
"a are in line the these words"

Thanks!
perl -e '{$_="these are the words in a line"; s/(.*) (.*) (.*) (.*) (.*) (.*) (.*)/$6 $2 $5 $7 $3 $1 $4/; print}'
# 3  
Old 06-17-2009
Hammer & Screwdriver Maybe this approach

Code:
> echo "these are the words in a line" | tr " " "\n" | sort | tr "\n" " " ;echo
a are in line the these words

That last echo command is to simply force a new line.
# 4  
Old 06-17-2009
Quote:
Originally Posted by joeyg
Code:
> echo "these are the words in a line" | tr " " "\n" | sort | tr "\n" " " ;echo
a are in line the these words

That last echo command is to simply force a new line.
nice one. although my line will have more than 9 terms, seperated by commas

e.g
csoTopic=Topics: Artists Exact Match, queryTerm=new kids on the block, defaultDisplayName=New Kids On The Block, topic=vid
eos, artistName=New Kids On The Block, weight=50,

I think Im gonna have to go down the road of splitting them in a temp file , ordering them, and joining them up again.


edited... this was in response to jimmy_y's fine perl skills.
# 5  
Old 06-17-2009
echo "these are the words in a line"|awk -F- '{print $6 $2 $5 $7 $3 $1 $4}'

echo "these are the words in a line"|sed 's/\(.*\) \(.*\) \(.*\) \(.*\) \(.*\) \(.*\) \(.*\)/\6 \2 \5 \7 \3 \1 \4/g'

from
https://www.unix.com/shell-programmin...-one-line.html
# 6  
Old 06-17-2009
Quote:
Originally Posted by jimmy_y
echo "these are the words in a line"|awk -F- '{print $6 $2 $5 $7 $3 $1 $4}'

echo "these are the words in a line"|sed 's/\(.*\) \(.*\) \(.*\) \(.*\) \(.*\) \(.*\) \(.*\)/\6 \2 \5 \7 \3 \1 \4/g'

from
https://www.unix.com/shell-programmin...-one-line.html
I thought the OP wanted to sort the fields - not simply manually rearrange them!
# 7  
Old 06-17-2009
Quote:
Originally Posted by vgersh99
I thought the OP wanted to sort the fields - not simply manually rearrange them!
Smilie Did not realize he wants to sort instead of manual arrange.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

4. Shell Programming and Scripting

How to sort lines according words?

Hello I greped some lines from an xml file and generated a new file. but some entries are missing my table is unsorted. e.g. NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" NUMBER="2" POSTCODE="33333"... (5 Replies)
Discussion started by: witchblade
5 Replies

5. Shell Programming and Scripting

How can I sort by n number is like words?

I want to sort a file with a list of words, in order of most occuring words to least occurring words as well as alphabetically. ex: file1: cat 3 cat 7 cat 1 dog 3 dog 5 dog 9 dog 1 ape 4 ape 2 I want the outcome to be: file1.sorted: dog 1 (12 Replies)
Discussion started by: castrojc
12 Replies

6. UNIX for Dummies Questions & Answers

words sort

hello guys i need a command that take the words from multiple files and put them in another file this way: one word needs to appear only once in the destination file with small letters no matter how it appears in source files , the words from destination file needs to be alphabetical ordered and... (10 Replies)
Discussion started by: G30
10 Replies

7. UNIX for Dummies Questions & Answers

Trying to sort words and numbers associated with them.

Hi. I have a file containing words and numbers associated with them as follows - c 2 b 5 c 5 b 6 a 10 b 16 c 18 a 19 b 21 c 27 a 28 b 33 a 76 a 115 c 199 c 251 a 567 a 1909 (4 Replies)
Discussion started by: maq
4 Replies

8. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

9. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

10. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies
Login or Register to Ask a Question