![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Calculating field using AWK, or GAWK script | Trellot | UNIX for Dummies Questions & Answers | 4 | 10-28-2007 12:43 PM |
| gawk will work or not ? | tkbharani | Shell Programming and Scripting | 3 | 03-13-2007 03:26 PM |
| gawk HELP | sandeep_hi | Shell Programming and Scripting | 6 | 06-19-2006 05:56 AM |
| how to use variables and gawk in a script? | pau | Shell Programming and Scripting | 9 | 05-29-2006 06:39 AM |
| rs and ors in gawk ...???? | moxxx68 | Shell Programming and Scripting | 2 | 10-05-2004 09:52 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
gawk script
Hey guys need your help with an gawk script... here's what I have so far
gawk '^d/ {printf "%-20s %-10s %-10s %-10s %-4s%2s %5s\n",$9,$1,$3,$4,$6,$7,$8}' ls.kbr The file ls.kbr is a capture of 'ls-al' What I want gawk to do is: 1) Find only directories (this is working) 2) skip lines beginning containing . and .. directories 3) skip lines where the directory name is longer than 8 characters 4) determine the longest length of each field, and use that length as a width specifier for each %s in printf(). You can see I'm hardcoding the length for now. TIA Zoo591 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
ls -la | awk '/^d/ && length($9) <= 8 {
if ( $9 == "." || $9 == "..") next;
F[++j]=$0;
if (max1 < length($9)) max1=length($9);
if (max2 < length($1)) max2=length($1);
if (max3 < length($3)) max3=length($3);
if (max4 < length($4)) max4=length($4);
if (max5 < length($6)) max5=length($6);
if (max6 < length($7)) max6=length($7);
if (max7 < length($8)) max7=length($8);
}
END{ for (i=1; i<=j; i++)
{
split(F[i], M);
printf "%-*s %-*s %-*s %-*s %-*s%*s %*s\n", max1, M[9], max2, M[1], max3, M[3], max4, M[4], max5, M[6],max 6, M[7],max7, M[8]
}
}'
Quote:
|
|
#3
|
|||
|
|||
|
Nicely done. You are numero uno AWK!
Quote:
|
|||
| Google The UNIX and Linux Forums |