Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-15-2012
Registered User
 
Join Date: Mar 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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 to find the longest word???

Any help much appreciated, thanks.

Last edited by scotty85; 03-15-2012 at 06:00 PM..
Sponsored Links
    #2  
Old 03-15-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,454
Thanks: 108
Thanked 498 Times in 477 Posts
Use tr to read the file and translate all spaces into newlines. Pipe to wc and use the -L option that reports the longest line.

As this smells a bit like homework, I'm not going to provide the exact command -- if it's not homework, then say so and someone will provide more details I'm sure.

(and as a side note, since I see it done way too often, there is no need to use cat to do this!)

Last edited by agama; 03-15-2012 at 06:10 PM.. Reason: added opnion :)
Sponsored Links
    #3  
Old 03-15-2012
Registered User
 
Join Date: Dec 2004
Location: Isle-of-Skye
Posts: 231
Thanks: 7
Thanked 26 Times in 26 Posts
Am I missing something here, why do you want to use the 'tr' command?

Regards

Dave
    #4  
Old 03-15-2012
Registered User
 
Join Date: Mar 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
yes its homework, i am an absolute unix beginner so no nothing.

if you were able to tell me how i would go about approaching this kind of question because i genuinely have no idea :s that would be awesome then ill try figure out a command from there.

tr and wc were just suggested commands to use
Sponsored Links
    #5  
Old 03-15-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,383
Thanks: 491
Thanked 2,535 Times in 2,418 Posts
It's an easy way to make each individual word its own line. Then you just hunt for the longest line.
Sponsored Links
    #6  
Old 03-15-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,383
Thanks: 491
Thanked 2,535 Times in 2,418 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Display all the words whose length is equal to the longest word in the text bawse.c UNIX for Dummies Questions & Answers 4 08-14-2011 06:32 AM
To read data word by word from given file & storing in variables sjoshi98 Shell Programming and Scripting 2 06-09-2010 05:31 AM
find a word in a file, and change a word beneath it ?? vikas027 Shell Programming and Scripting 2 02-13-2008 03:23 PM
in sed ,to get longest word lakshmananindia UNIX for Advanced & Expert Users 6 09-26-2007 12:28 PM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 06:29 AM



All times are GMT -4. The time now is 07:30 PM.