Finding the length of the longest column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding the length of the longest column
# 1  
Old 11-14-2012
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:

Code:
awk '{print NF; exit}'  file


Now, I can do this:

Code:
awk '{ if (length($0) > max) max = length($0) } END { print max }' file

but that prints the length of the longest line in the file rather than the length of the longest field in the file.

Any help please?

Thanks!
# 2  
Old 11-14-2012
Try
Code:
awk '{for (i=1;i<=NF;i++) if (length($i)>max) max=length($i)} END{print max}' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-14-2012
Quote:
Originally Posted by MIA651
Code:
awk '{ if (length($0) > max) max = length($0) } END { print max }' file

That can do the job if your AWK implementation treats a multicharacter RS as a regular expression and if you set RS to a regular expression which mimicks the default value of FS. In effect, what is usually a field becomes a record.

If your AWK does not support that RS behavior, the same can be accomplished by filtering the file through tr, replacing spaces and tabs with newlines, before piping into AWK.

I'll leave the actual code as an exercise.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 11-14-2012
Hi, in awk $0 is the whole line. If you want only one field of the line you have to check for i=1 to NF.
$0 = $1 + $2 +.....NF. You have to know the length of $1 ,$2.... and compare them. For example with a loop while...while (i<NF) and you have to keep the minor length while the loop checks every field...If you need help with the code ask me for it.
# 5  
Old 11-14-2012
danielos,
I am aware that $0 was the entire line, I used $NF though and it gave me the length of the last column.

Alistar,
That sounds like a good plan, I'll give that a shot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

Need to extract data from Column having variable length column

Hi , I need to extract data from below mentioned data, having no delimiter and havin no fixed column length. For example: Member nbr Ref no date 10000 1000 10202012 200000 2000 11202012 Output: to update DB with memeber nbr on basis of ref no. ... (6 Replies)
Discussion started by: ns64110
6 Replies

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

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

6. Shell Programming and Scripting

Finding longest line in a Record

Good Morning/Afternoon All, I am using the nawk utility in korn shell to find the longest field and display that result. My Data is as follows: The cat ran The elephant ran Milly ran too We all ran I have tried nawk '{ if (length($1) > len) len=length($1); print $1}' filename The... (5 Replies)
Discussion started by: SEinT
5 Replies

7. UNIX for Dummies Questions & Answers

How to remove duplicated based on longest row & largest value in a column

Hii i have a file with data as shown below. Here i need to remove duplicates of the rows in such a way that it just checks for 2,3,4,5 column for duplicates.When deleting duplicates,retain largest row i.e with many columns with values should be selected.Then it must remove duplicates such that by... (11 Replies)
Discussion started by: reva
11 Replies

8. Shell Programming and Scripting

Finding multiple column values and match in a fixed length file

Hi, I have a fixed length file where I need to verify the values of 3 different fields, where each field will have a different value. How can I do that in a single step. (6 Replies)
Discussion started by: naveen_sangam
6 Replies

9. Shell Programming and Scripting

Finding longest common substring among filenames

I will be performing a task on several directories, each containing a large number of files (2500+) that follow a regular naming convention: YYYY_MM_DD_XX.foo_bar.A.B.some_different_stuff.EXT What I would like to do is automatically discover the part of the filenames that are common to all... (1 Reply)
Discussion started by: cmcnorgan
1 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