Printf awk: argument to adjust automatically width to the largest value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Printf awk: argument to adjust automatically width to the largest value
# 1  
Old 03-09-2012
Printf awk: argument to adjust automatically width to the largest value

Hi !

Here is an example of a tab-delimited file with variable width values in field 2 (space separated values).
As the real file contains lot of lines, I can adjust manually the width to the largest value in field 2.
Does it exist an argument to do it automatically?

input.tab:
Code:
field1	field2	field3
a1	bbbbbb1	cccccc1
dd2	ddd ddddd dddddddddddd2	eeee2
ffff3	gggggg gg g g g gg3	h3

It works if I use this command because I can see the approximative width in this example:
Code:
awk -F"\t" '{printf "%-s\t%-30s%-s\n",$1,$2,$3}' input.tab

output.tab:
Code:
field1  field2                        field3
a1      bbbbbb1                       cccccc1
dd2     ddd ddddd dddddddddddd2       eeee2
ffff3   gggggg gg g g g gg3           h3

I am looking for an argument to find the width (here "30") automatically.

Thanks for your help !
# 2  
Old 03-09-2012
what is the field delimiter ?
# 3  
Old 03-09-2012
Fields are tab-delimited.
# 4  
Old 03-09-2012
Code:
nawk -F"\t" 'BEGIN{b=0}{for(i=1;i<=NF;i++){a=length($i);if(a>b)b=a;}}END{print b}' input.txt

the above awk will give the longest field in your file.

after that you can pass that value to awk and pass it to the printf
This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 03-09-2012
This will check for the largest length in every column and will space accordingly.

Code:
perl -F'\t' -lane '
BEGIN {
    open I, "< inputfile";
    for (<I>) {
        chomp;
        @x = split /\t/;
        (length ($x[0]) > $f0) && ($f0 = length ($x[0]) + 1);
        (length ($x[1]) > $f1) && ($f1 = length ($x[1]) + 1);
        (length ($x[2]) > $f2) && ($f2 = length ($x[2]) + 1);
    }
    close I;
}
print "$F[0]" . " " x ($f0 - length ($F[0])), "$F[1]" . " " x ($f1 - length ($F[1])), "$F[2]" . " " x ($f2 - length ($F[2]));
' inputfile

This User Gave Thanks to balajesuri For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to adjust text and count based on value in field

The below awk executes as is and produces the current output. It isvery close but what Ican not seem to do is add the -exon..., the ... portion comes from $1 and the _exon is static and will never change. If there is + sign in $4 then the ... is in acending order or sequential. If there is a - in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

awk to combine matches and use a field to adjust coordinates in other fields

Trying to output a result that uses the data from file to combine and subtract specific lines. If $4 matches in each line then the last $6 value is added to $2 and that becomes the new$3. Each matching line in combined into one with $1 then the original $2 then the new$3 then $5. For the cases... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Using printf (or other?) to create variable fixed width text

I would like to use printf (or something else?) to create a line of text that has varying column widths. This will be used to create a fixed width file (with varying column widths). For example, consider variables $1 $2 $3 are equal to a, b, c respectively and they should be printed in column... (10 Replies)
Discussion started by: farrenthorpe
10 Replies

6. Shell Programming and Scripting

awk second largest, third largest value

I have two text files like this: file1.txt: 133 10 133 22 133 13 133 56 133 78 133 98 file2.txt: 158 38 158 67 158 94 158 17 158 23 I'm basically trying to have awk check the second largest value of the second column of each text file, and cat it to its own text file. There... (13 Replies)
Discussion started by: theawknewbie
13 Replies

7. Shell Programming and Scripting

AWK (how) to get smallest/largest nr of ls -la

Hey, This is a long-shot however, I am stuck with the following problem: I have the output from ls -la, and I want to sort some of that data out by using AWK to filter it. ls -la | awk -f scriptname.awk Input: For example: drwxr-xr-x 3 user users 4096 2010-03-14 20:15 bin/... (5 Replies)
Discussion started by: abciscool
5 Replies

8. Shell Programming and Scripting

largest field , awk , help

Hi All, My file is like this: $ cat max.txt abcd:1982:a efghij:1980:e klmn:1923:k opqrst:1982:o I have to find out the largest first field and the corresponding line. i.e Output required: efghij efghij:1980:e opqrst opqrst:1982:o HTH, jkl_jkl (6 Replies)
Discussion started by: jkl_jkl
6 Replies

9. UNIX for Dummies Questions & Answers

Fixed Width file using AWK

I am using the following command at the Unix prompt to make my 'infile' into a fixed width file of 100 characters. awk '{printf "%-100s\n",$0}' infile > outfile However, there are some records with a special character "©" These records are using 3 characters in place of one and my record... (2 Replies)
Discussion started by: alok.benjwal
2 Replies

10. Programming

fixed-width printf() output on an XmList on Solaris

This one is a bit of bugger and I'm not sure if it's entirely relevant to this board but here goes... I'm trying to produce some fixed width output to a List widget (X11/Motif GUI) So, I use something similar to: sprintf(Buffer, "%*d%*f", 20, some_integer, 20, some_float); This gives me a... (0 Replies)
Discussion started by: trido
0 Replies
Login or Register to Ask a Question