Display all the words whose length is equal to the longest word in the text

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Display all the words whose length is equal to the longest word in the text
# 1  
Old 08-14-2011
Data Display all the words whose length is equal to the longest word in the text

Hi Guys,

I was going some trial and error to see if I can find the longest word in a text.

I was using Pipes because they are easier to use in this case.
I was stuck on this for a while so I thought I'll get some help with it.

I tried this code to separate all the words in a text in lines and this would give me the LENGTH of the longest word.
tr -s " .,;" "\n" <random.txt | tr '[A-Z]' '[a-z]' | sort -u | wc -L

But suppose if I want to display the longest word/s according to length (for example, longest word is 15 characters long and I want to display all words with the length of 15 characters).

I tried using this command but doesnt work

tr -s " .,;" "\n" <random.txt | tr '[A-Z]' '[a-z]' | sort -u | uniq -u

# 2  
Old 08-14-2011
It's impossible to do with pipes - you need read all lines to find out the maximum length and then print saved lines which matches the maximum. Awk is a good tool for it - you read lines, save those with the current maximum and after processing you print all lines with the real maximum:
tr -s " .,;" "\n" <random.txt | awk 'length($0) >= max { a[$0]++; max = length($0) }
  for (s in a) if (length(s) == max) print s

Or a little better:
... awk '                                          
length($0)  > max { a[$0]++; max = length($0) }
length($0) == max { a[$0]++ }
END {                                       
  for (s in a) if (length(s) == max) print s

Last edited by yazu; 08-14-2011 at 07:07 AM.. Reason: Corrected a bug and added a better version
# 3  
Old 08-14-2011
it is possible to do it with pipes.
This will pretty much need 3 commands to complete

tr, wc and the command pipe should be able to do it. My lecturer was saying something about it
# 4  
Old 08-14-2011
I will be happy to see. Bonus fortuna.
# 5  
Old 08-14-2011
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter all the lines with minimum specified length of words of a text file

Hi Can someone tell me which script will work best (in terms of speed and simplicity to write and run) for a large text file to filter all the lines with a minimum specified length of words ? A sample script with be definitely of great help !!! Thanks in advance. :) (4 Replies)
Discussion started by: my_Perl
4 Replies

2. Shell Programming and Scripting

wc -L giving incorrect length of longest line

Running below line gives 3957 as length of longest line in file 20121119_SRMNotes_init.dat awk ' { if ( length > 3950 ) { x = length } }END{ print x }' 20121119_SRMNotes_init.dat While wc -L 20121119_SRMNotes_init.dat gives output as 4329. Why is there a difference between these two commands.... (2 Replies)
Discussion started by: Satish Mantha
2 Replies

3. Shell Programming and Scripting

Finding the length of the longest column

Hi, I am trying to figure out how to get the length of the longest column in the entire file (because the length varies from one row to the other) I was doing this at first to check how many fields I have for the first row: awk '{print NF; exit}' file Now, I can do this: awk '{ if... (4 Replies)
Discussion started by: MIA651
4 Replies

4. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

5. Shell Programming and Scripting

Longest word in a file

I am trying to write a command on just one line, i.e seperated by ';' and '|' etc, that finds the number of characters in the longest word of a file, preferably using the 'tr' and 'wc' commands. i no that wc shows the number of lines words and characters in a file but im not sure how to use it... (5 Replies)
Discussion started by: scotty85
5 Replies

6. Shell Programming and Scripting

Longest length of string in array

I would be grateful if someone could help me. I am trying to write a .sh script in UNIX. I have the following code; User=john User=james User=ian User=martin for x in ${User} do print ${#x} done This produces the following output; 4 5 3 6 (12 Replies)
Discussion started by: mmab
12 Replies

7. Shell Programming and Scripting

Display text between two words/characters

Using sed or awk, I need to display text between two words/characters. Below are two example inputs and the desired output. In a nutshell, I need the date-range value between the quotes (but only the first occurance of date-range as there can be more than one). Example One Input: xml-report... (1 Reply)
Discussion started by: cmichaelson
1 Replies

8. Shell Programming and Scripting

Display most top 10 occurring words along with number of ocurences of word inthe text

I need Display the most top 10 occurring words along with the number of occurences of those words in the given text. Sample text as below: "The Travails of Single South Indian men of conservative upbringing" or "Why we don't get any..." Yet another action packed weekend in Mumbai, full of... (2 Replies)
Discussion started by: smacherla
2 Replies

9. Shell Programming and Scripting

Find the length of the longest line

Dear All, To find the length of the longest line from a file i have used wc -L which is giving the proper output... But the problem is AIX os does not support wc -L command. so is there any other way 2 to find out the length of the longest line using awk or sed ? Regards, Pankaj (1 Reply)
Discussion started by: panknil
1 Replies

10. UNIX for Advanced & Expert Users

in sed ,to get longest word

i want the longest word from the file using sed. can any one help me in this case? (6 Replies)
Discussion started by: lakshmananindia
6 Replies
Login or Register to Ask a Question