Find longest string and print it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find longest string and print it
# 1  
Old 04-20-2011
Find longest string and print it

Hello all,

I need to find the longest string in a select field and print that field.

I have tried a few different methods and I always end up one step from where I need to be.

Methods thus far:
Code:
nawk '{if (length($1) > long) long=length($1); if(length($1)==long) print $1}'

The above print my desired result and a few other lines. I have been thinking of a way I could have a variable that holds only the largest value then test that value against lenght($1) and print $1 if they are equal but I have had no luck.

I have also tried placing all the length($1) values into an array, sorting and printing the array but I am short one step which is to take the first values in the array (the highest value) and test it against length($1) and print $1 if they are equal.

Code:
nawk '{a[NR]=length($1);  print a[NR] | "sort +0nr -2 +2d"}'

I apologize if my logic is a bit messy, this is my 5th day playing with nawk.
Thanks in advance
# 2  
Old 04-20-2011
Is this what you're looking for?
Code:
awk '!len || length($1) > len {len=length($1)} END{print len}' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 04-20-2011
Quote:
Originally Posted by Franklin52
Is this what you're looking for?
Code:
awk '!len || length($1) > len {len=length($1)} END{print len}' file

This is very close. Based on one of the prior code I managed to put together, the final step would be taking this output and using it in the following manner.

Theoretically speaking.
Code:
awk '!len || length($1) > len {len=length($1)} END{print len}' file
result
awk '{if (length($1) == result) print $1}'

I will try this with via a for loop, the final index of len or a pipe once I am awake and let you know what the final code looks like.

Thank you Franklin.
# 4  
Old 04-20-2011
If you want to print the longest field you could do something like:
Code:
awk '!len || length($1) > len {len=length($1);s=$1} END{print s, len}' file

# 5  
Old 04-20-2011
Quote:
Originally Posted by Franklin52
If you want to print the longest field you could do something like:
Code:
awk '!len || length($1) > len {len=length($1);s=$1} END{print s, len}' file

Yes, this is what I was trying to do.

Can you please break down the logic for me?
Primarily the
Code:
 !len ||

# 6  
Old 04-20-2011
Quote:
Originally Posted by SEinT
Yes, this is what I was trying to do.

Can you please break down the logic for me?
Primarily the
Code:
 !len ||

If the variable len is 0 or is "".
# 7  
Old 04-20-2011
Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace substring by longest string in common field (awk)

Hi, Let's say I have a pipe-separated input like so: name_10|A|BCCC|cat_1 name_11|B|DE|cat_2 name_10|A|BC|cat_3 name_11|B|DEEEEEE|cat_4 Using awk, for records with common field 2, I am trying to replace all the shortest substrings by the longest string in field 3. In order to get the... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

Parse the longest matching string

Hello experts, I am trying to unscramble a mixed signal into component signals. Let the list of known signals be $ cat tmplist DU DU4016 GFF GFF2010 GFF201019 G2115 G211 DU40 (1 Reply)
Discussion started by: senhia83
1 Replies

3. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

4. Shell Programming and Scripting

awk find and print next string

Can I do this in one awk session. Solution I have is poor. I want to return the number after PID. echo "Start: 12345 is used by PID:11111 username" | awk -F: '{print $3}' | awk '{print $1}' (6 Replies)
Discussion started by: u20sr
6 Replies

5. Shell Programming and Scripting

awk uniq and longest string of a column as index

I met a challenge to filter ~70 millions of sequence rows and I want using awk with conditions: 1) longest string of each pattern in column 2, ignore any sub-string, as the index; 2) all the unique patterns after 1); 3) print the whole row; input: 1 ABCDEFGHI longest_sequence1 2 ABCDEFGH... (12 Replies)
Discussion started by: yifangt
12 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. Programming

Find and print number after string in C

I'm trying find and print a number after a specific user passed string in each line of a text file using C (as requested by the powers that be). I've pieced together enough to read the file, find the string and print the line it was found on but I’m not sure where to even start in terms of finding... (3 Replies)
Discussion started by: cgol
3 Replies

8. UNIX for Dummies Questions & Answers

find string and and print another string

i have a file that looks like this ABC123 aaaaaaaaaaaaaaasssssssssssssssffhhh ABC234 EMPTY ABC652 jhfffffffffffffffffffffffffffffffffffkkkkkkkkkkkk i want to grep "EMPTY" and print ABC234 (3 Replies)
Discussion started by: engr.jay
3 Replies

9. Shell Programming and Scripting

Shell script to find longest phrase

Hi Everyone, I am trying to write a shell script that can find the longest phrase that appears at least twice in an online news article. The HTML has been parsed through an HTML parser, converted to XML and the article content extracted. I have put this article content in a text file to work... (24 Replies)
Discussion started by: stargazerr
24 Replies

10. 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
Login or Register to Ask a Question