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 and shell scripting languages 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 03:43 PM
gawk will work or not ? tkbharani Shell Programming and Scripting 3 03-13-2007 06:26 PM
gawk HELP sandeep_hi Shell Programming and Scripting 6 06-19-2006 09:56 AM
how to use variables and gawk in a script? pau Shell Programming and Scripting 9 05-29-2006 10:39 AM
rs and ors in gawk ...???? moxxx68 Shell Programming and Scripting 2 10-05-2004 01:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 05-28-2008
zoo591 zoo591 is offline
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
  #2 (permalink)  
Old 05-28-2008
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134

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
  #3 (permalink)  
Old 05-28-2008
zoo591 zoo591 is offline
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]
   }
}'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0