The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 05-28-2008
Registered User
 

Join Date: Jul 2005
Posts: 20
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
Reply With Quote
Forum Sponsor
  #2  
Old 05-28-2008
awk awk is offline
Registered User
 

Join Date: Feb 2007
Posts: 119
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:
Originally Posted by zoo591 View Post
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
Reply With Quote
  #3  
Old 05-28-2008
Registered User
 

Join Date: Jul 2005
Posts: 20
Nicely done. You are numero uno AWK!

Quote:
Originally Posted by awk View Post
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]
   }
}'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0