Sort words based on word count on each line


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sort words based on word count on each line
# 1  
Old 06-01-2015
Question Sort words based on word count on each line

Hi Folks Smilie

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:
Code:
word
word word word
word word
word word word word
word
word word word
word word


to desired output:
Code:
word
word
word word
word word
word word word
word word word
word word word word


Any help would be greatly appreciated.
Thanks for your help!

ps. Hope i posted this in the correct forum board. My apologies if it's in the wrong section

Last edited by Don Cragun; 06-01-2015 at 05:08 AM.. Reason: Add CODE tags.
# 2  
Old 06-01-2015
Assuming you don't have more than 99,999 words on a line and that words are separated by sequences of one or more blanks, the following should work:
Code:
#!/bin/ksh
awk '{printf("%05d%s\n", NF, $0)}' file.txt|sort|awk '{print substr($0, 6)}'

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 06-01-2015
Quote:
Originally Posted by Don Cragun
Assuming you don't have more than 99,999 words on a line and that words are separated by sequences of one or more blanks, the following should work:
Code:
#!/bin/ksh
awk '{printf("%05d%s\n", NF, $0)}' file.txt|sort|awk '{print substr($0, 6)}'

Thank you Don! It works perfectly. Just what I needed. Many Thanks
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. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

3. UNIX for Advanced & Expert Users

cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed. https://www.unix.com/shell-programming-scripting/105841-count-words-each-line-file-using-xargs.html If the line has 6 words I would like to use this.... (8 Replies)
Discussion started by: cokedude
8 Replies

4. Shell Programming and Scripting

Count and print all repeating words in a line

Gurus, I have a file containing lines like this : Now, number of words in each line varies. My need is, if a word repeats in a line get it printed. Also total number of repeats. So, the output would be : Any help would be highly appreciated. Thanks & Regards (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

5. Shell Programming and Scripting

How to remove all words from a matching word in a line?

Hi Guys, :p I have a file like this: 2010-04-25 00:00:30,095 INFO - ]- start process U100M4 2010-04-25 00:00:30,096 DEBUG - ] -- call EJB 2010-04-25 00:00:30,709 INFO - - end processU100M4 2010-04-25 00:00:30,710 DEBUG - got message=Sorry I want to out put format. 2010-04-25... (5 Replies)
Discussion started by: ooilinlove
5 Replies

6. Shell Programming and Scripting

How to fetch rows based on line numbers or based on the beginning of a word?

I have a file which will have rows like shown below, ST*820*316054716 RMR*IV*11333331009*PO*40.31 REF*IV*22234441009*xsss471-2762 DTM*003*091016 ENT*000006 RMR*IV*2222234444*PO*239.91 REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN> DTM*003* 091016 RMR*IV*2223344441009*PO*40.31... (18 Replies)
Discussion started by: Muthuraj K
18 Replies

7. Shell Programming and Scripting

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! (15 Replies)
Discussion started by: rebelbuttmunch
15 Replies

8. Shell Programming and Scripting

Count words on each line in file using xargs

Hi, im having a problem with xargs, i want to cout word of each line in file, and i HAVE to use xargs, i tried: cat file | xargs wc -w .....that uses all words in file like name of files and passed then to wc so it worte wc :somewordformfile is not i afile or directory cat file | xargs -I{} wc... (3 Replies)
Discussion started by: Qwetek
3 Replies

9. Shell Programming and Scripting

count no of words in a line

hi i have a line "abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc" I want to print the no of words, words separated by comma please help (3 Replies)
Discussion started by: Satyak
3 Replies

10. Shell Programming and Scripting

count no of words in a line

hi i have a string like str=abc def ghi jkl now i want to count the no of words in the string please help (7 Replies)
Discussion started by: satish@123
7 Replies
Login or Register to Ask a Question