Awk&Gawk

command line

awk 'BEGIN{v_list="hello good well done";n=split(v_list,ch);i=1;while(i<=n){print ch[i];i++}}'

Tips

awk -F[分隔符集] '{print $1}' filename     #使用两个分隔符
awk '{sum+=$1}; END{print sum}' #求和

AWK Array Script To Find File Maximum, Minimum and Total File Sizes

#!/bin/bash
# Written by Vivek Gite <www.cyberciti.biz>
# AWK to list total file size, minimum, maximum and other size using Arrays
# --------------------------------------------------------------------------
# Copyright (C) 2007 nixCraft project <http://www.cyberciti.biz/tips/contact-us>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# http://bash.cyberciti.biz/file-management/awk-array-script-to-find-file-maximum-minimum-and-total-file-sizes/
# -------------------------------------------------------------------------
 
dirs="$@"
 
[ $# -eq 0 ] && { echo "Usage: $0 dir1 dir2 dir N"; exit 999; }
 
for d in $dirs
do
    [ ! -d $d ] && { echo "** Error: $d is not directory"; continue; }
 
    echo "*** Info for $d directory ***"
    ls -Al $d | egrep -wv "^total"  |awk 'BEGIN{c=0;max=0;min=0;total=0}
 
            {arr[c]=$5;c++;total+=$5}
 
            END{
                  for (x = 0; x <= (c-1); x++){
                        if ( arr[x] > max ) max = arr[x];
                     }
                   min=max;
                  for (x = 0; x <= (c-1); x++){
                        if ( arr[x] < min ) min = arr[x];
                     }
            print "Max size:",max, "\nMin size:", min, "\nTotal size:", total}'
done

Gawk

array2html script

#!/usr/bin/gawk -f
# ___  _  _ ____ ___ ___ ____ ___  ____
# |  \ |  | |     |   |  |__| |__] |___
# |__/ |__| |___  |   |  |  | |    |___
#
# The scripts were written to be usefull in
# a research enviornment, but anyone is welcome
# to use them.  Happy awking.  -Tim Sherwood
 
BEGIN {
    FS =":";
    printf  "%s%s%s",
        "<TABLE cellpadding=\"1pt\" BORDER=\"2pt\" ",
        "CELLSPACING=\"0pt\" bgcolor=\"\#ffffff\" ",
        "bordercolor=\"\#000000\">\n";
 
}
 
(NR==1){
    printf "   <TR bgcolor=\"\#dfdfdf\">\n"
    for( i=1; i<=NF; i++ )
    {
        printf "      <TD><center>%s</center></TD>\n", $i;
    }
    printf "   </TR>\n"
}
 
(NF>0 && NR>1){
 
    printf "   <TR>\n"
    for( i=1; i<=NF; i++ )
    {
        if ( i==1 ) {
            printf "      <TD align=left>%s</TD>\n", $i;
        } else {
            printf "      <TD align=right>%s</TD>\n", $i;
        }
    }
    printf "   </TR>\n"
}
 
END {
    printf "</TABLE>\n";
}
/var/lib/openshift/bccd8eac1968476490eaee9ced33c7bf/app-root/runtime/repo/php/data/pages/awk.txt · 最后更改: 2012/11/12 14:08 由 admin
到顶部
CC Attribution-Noncommercial-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0