Work out max variable lengths in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Work out max variable lengths in file
# 1  
Old 03-05-2012
Work out max variable lengths in file

I have a pipe dilminted txt file with 30 variables in it. How can I calculate the maximum number of characters in each variable in the file.
# 2  
Old 03-05-2012
Quote:
Originally Posted by RobWork
I have a pipe dilminted txt file with 30 variables in it. How can I calculate the maximum number of characters in each variable in the file.
Code:
$
$ cat f36
12345|abcd|6666|XYZ
234|defghij|77777|CYXP
456|mnopqr|8888|TYSICWROKM
$
$
$ perl -F"\|" -lane 'if ($. == 1) {@x = map {length} @F}
                     else {@x = map {length($F[$_]) > $x[$_] ? length($F[$_]) : $x[$_]} (0..$#F)}
                     END {
                       foreach $i (0..$#x) {print "Max length of variable ",($i+1)," is $x[$i]"}
                     }' f36
Max length of variable 1 is 5
Max length of variable 2 is 7
Max length of variable 3 is 5
Max length of variable 4 is 10
$
$

tyler_durden

Last edited by durden_tyler; 03-05-2012 at 09:44 PM..
# 3  
Old 03-06-2012
awk:
Code:
awk -F\| '{for(i=1;i<=NF;i++){l=length($i); if (l>L[i])L[i]=l}} END{for(i=1;i<=NF;i++) print i,L[i]}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ldapsearch using variable will not work

When I execute the code below with cn set to the $adgroup variable, I get the following error: Invalid DN syntax (34) Additional information: 0000208F: NameErr: DSID-031001F7, problem 2006 (BAD_NAME), data 8349, best match of: ,ou=Resource,ou=groups,dc=abc,dc=somecompany,dc=com' If I set cn... (6 Replies)
Discussion started by: who10
6 Replies

2. UNIX for Advanced & Expert Users

UTF-8,16,32 character lengths using awk

Hi All, I am trying to obtain count of characters using awk, but "length" function returns a value of 1 for 2-byte or 3-byte characters as well unlike wc -c command. I have tried to use the below commands within awk function, but it does not seem to work { cmd="wc -c "stringtocheck ( cmd )... (6 Replies)
Discussion started by: tostay2003
6 Replies

3. Shell Programming and Scripting

Paste files of varying lengths

I have three files of varying lengths and different number of columns. How can I paste all three with all columns aligned? File1 ---- 123 File2 ---- 234 345 678 File3 ---- 456 789 Output should look like: 123 234 456 345 789 (6 Replies)
Discussion started by: Un1xNewb1e
6 Replies

4. Shell Programming and Scripting

Merge two files with different lengths

Hi there, I have two very long files like: file1: two fields 1 123 1 125 1 234 2 123 2 234 2 300 2 312 3 10 3 215 4 56 ... (11 Replies)
Discussion started by: ClaraW
11 Replies

5. Shell Programming and Scripting

Merging data from 2 files of different lengths?

Hi all, Sorry if someone has answered something like this already, but I have a problem. I am not brilliant with "awk" but think it should be the command to use to get what I am after. I have 2 files: job-file (several hundred lines like): 1018003,LONG MU WAN,1113S 1018004,LONG MU... (4 Replies)
Discussion started by: sgb2301
4 Replies

6. Shell Programming and Scripting

What's the max integer a variable can hold?

I would like to know the maximum integer that a variable can hold. Actually one of my variable holds value 2231599773 and hence the script fails to process it.Do we have any other data type or options available to handle this long integers? (9 Replies)
Discussion started by: michaelrozar17
9 Replies

7. Shell Programming and Scripting

Read lines with different lengths in while loop

Hi there ! I need to treat files with variable line length, and process the tab-delimited words of each line. The tools I know are some basic bash scripting and sed ... I haven't got to python or perl yet. So my file looks like this obj1 0.01953 0.34576 0.04418 0.01249 obj2 0.78140... (7 Replies)
Discussion started by: jossojjos
7 Replies

8. UNIX for Dummies Questions & Answers

Using grep to find strings of certain lengths?

I am trying to use grep to find strings of certain lengths that all start with the same letter. Is this possible?:confused: (4 Replies)
Discussion started by: crabtruck
4 Replies

9. Shell Programming and Scripting

Max size of variable

What is the maximum amount of characters that you can have in a varible name in the ksh shell? (1 Reply)
Discussion started by: lesstjm
1 Replies
Login or Register to Ask a Question