in sed ,to get longest word

Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users in sed ,to get longest word
# 1  
Old 09-26-2007
Question in sed ,to get longest word

i want the longest word from the file using sed.
can any one help me in this case?
# 2  
Old 09-26-2007
Sure it can be done a lot simpler....

awk ' BEGIN {
                n = 1
                while ( n <= NF ) {
                    Y = length($n)
                    if ( Y > X ) {
                        X = Y
                        WORD = $n
             END {
                     print WORD
                 } ' file

This User Gave Thanks to Cranie For This Post:
# 3  
Old 09-26-2007
awk '{ if ( length >a ) { a = length; b=$0 }}END{ print b }' filename

# 4  
Old 09-26-2007
matrixmadhan - this will print the lonest line rather than the longest word though won't it?
# 5  
Old 09-26-2007
Originally Posted by lakshmananindia
i want the longest word from the file using sed.
can any one help me in this case?
What is your definition of a word?

Why is the solution required to be with sed?

What have you tried so far?

cheers, drl
# 6  
Old 09-26-2007
Or something like this with zsh/bash:

read< <(sort -rn <(for w in $(<file);do w="${w//[,.:\"\'()<>]}"
printf "%d %s\n" "${#w}" "$w";done));printf "%s\n" "${REPLY#* }"

For example:

bash 3.2.25(1)$ cat file
The Z shell (zsh) is a Unix shell that can be used as an interactive login shell 
and as a powerful command interpreter for shell scripting. Zsh can be thought of 
as an extended bourne shell with a large number of improvements, including some
of the most useful features of bash, ksh, and tcsh.
bash 3.2.25(1)$ read< <(sort -rn <(for w in $(<file);do w="${w//[,.:\"\'()<>]}"
printf "%d %s\n" "${#w}" "$w";done));printf "%s\n" "${REPLY#* }"

# 7  
Old 09-26-2007
awk 'BEGIN{ l=0}
	  for ( i=1 ;i<=NF;i++){
   		 if (length($i) >l ) { 
	  print l, f
   }' "file"

# ./
12 improvements

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

2. Shell Programming and Scripting

Remove word with sed

How can I use sed or any utility to remove any word that begins with TRS-, I have tried sed 's/ERA.*//g' but seems not to be working Input: 23 TRS-458-9 345 235 45 TRS-42-5 423 000 76 300 234 Output: 23 345 235 45 423 000 76 300 234 (5 Replies)
Discussion started by: aydj
5 Replies

3. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

4. 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

5. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

6. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: bawse.c
4 Replies

7. Shell Programming and Scripting

Extract word using sed

Hello, I am new to sed and am trying to extract a word using sed. for example i have a line "const TotalAmount& getTotalAmount() const; " in the file test.txt I am trying to extract getTotalAmount() from the line. For this i tried cat test.txt | sed -n 's/.*get*\(\)//p But... (8 Replies)
Discussion started by: prasbala
8 Replies

8. Shell Programming and Scripting

Whole word with sed?

Hi experts, need a help in SED file=counter.c module=abcd i=${module}/trunk/counter/${file} When i do echo ${i}| sed "s|${module}/trunk/||g"| sed "s|${file}||g" it results in ounter.c (5 Replies)
Discussion started by: ganga.dharan
5 Replies

9. Shell Programming and Scripting

Word wrap with sed

Hi, I got some timetable in a file but it is all mixed up like this 01:00 hgrtwhrt #104:00 tyergethr05:00 tqqrthd qrth #107:00 qhtrhqerth10:00 qerthrthqr qtrqthr qthrrt11:00 thqrthqrthrr rthgreth #212:00 trhrthrth14:00 wrthwrtwrqrthwrthwr #2116:00 trqhthtr: rthrthr17:00 rtwhtrhwrth rthwrt... (6 Replies)
Discussion started by: stinkefisch
6 Replies

10. Shell Programming and Scripting

whole word substitution in SED

I am trying to substitute something with sed and what I want is to substitute a whole word and not part of a word. ie sed 's/class/room/g' filename will substitute both class and classes into room and roomes which is not what i want Grep for instance can use the -w option or <> grep -w... (7 Replies)
Discussion started by: gikay01
7 Replies
Login or Register to Ask a Question