awk应用

雪糕 2010-12-19

a.txt
andy    05/99   48311   Green   8       40      44
congfeng        06/99   4317    green   9       24      26
ljb     02/99   48      Yellow  12      35      28
carl    07/99   4842    Brown-3 12      16      26
rich    05/99   4712    Brown-2 12      30      28


[root@localhost shell]# awk '{ print $0 }' a.txt
[root@localhost shell]# awk '{ print $1,$4}' a.txt

#单独使用begin/BEGIN都可以,但是要使用begin-end的方式。必须全部大写:BEGIN-END
[root@localhost shell]# awk 'BEGIN {print "hello, andylin!\nName\tBelt\n"}
[root@localhost shell]# awk 'begin {print "hello,andylin!\nName\tBelt"} {print $1, $4} 

END {print"end of awk"} ' a.txt

[root@localhost shell]# awk '{if ($4 ~ /Brown/ ) print $0}' a.txt

[root@localhost shell]# awk '$3=="48" {print $0}' a.txt
[root@localhost shell]# awk '$0 !~ /Brown/' a.txt
[root@localhost shell]# awk '{if($0 !~ /Brown/) print$0}' a.txt
[root@localhost shell]# awk '{if ($4 !="Brown-2") print$0}' a.txt
[root@localhost shell]# awk '{if($6<$7) print$0} ' a.txt

[root@localhost shell]# awk '{if($6 <= $7) print $0}' a.txt



[root@localhost shell]# awk '/[Gg]reen/' a.txt

[root@localhost shell]# awk '{if($0~/[Gg]reen/) print $0}' a.txt

[root@localhost shell]# awk '{if ($1=="andy" && $4=="Green") print $0}' a.txt
[root@localhost shell]# awk '{if ($1=="andy" || $4=="Green") print $0}' a.txt

awk 'BEGIN {print ARGC,"_" FS, "_"} {print NF,NR} ' a.txt


awk 'END{print NR,$NF,FILENAME}' a.txt


[root@localhost shell]# echo $PWD | awk -F/ '{print $NF}'
[root@localhost shell]# pwd | awk -F/ '{print $NF}'

[root@localhost shell]# awk '{name=$1;belts=$4; if (belts ~/Yellow/) print name " is 

belt " belts}' a.txt

[root@localhost shell]# awk 'BEGIN {BASELINE="27  "} {if($6 < BASELINE) print $0}' a.txt
[root@localhost shell]# awk 'BEGIN {BASELINE=27} {if($6 < BASELINE) print $0}' a.txt

[root@localhost shell]# awk '{if($1=="andy"){$1="aaa"}; print $1}' a.txt

[root@localhost shell]# awk '{if($1 > "cc")  print$1,$2}' a.txt
[root@localhost shell]# awk '{if($1 > "cc") {}; print$1,$2}' a.txt

[root@localhost shell]# awk 'BEGIN{print"Name\tDifference"} {if($6<$7){$8=$7-

$6;print$1,$8}}' a.txt

[root@localhost shell]# awk '{total += $6} END{print "total points:"total}' a.txt
[root@localhost shell]# awk '{total += $6}{print$0} END{print "total points:"total}' 

a.txt

[root@localhost shell]# ls -l | awk '/^[^d]/ {print $8"\t"$5} {total += $5} END{print 

"total KB:"total}'


[root@localhost shell]# awk 'gsub(/4842/,1111){print$0}' a.txt

[root@localhost shell]# awk 'gsub(/4842/,1111){print$0}' a.txt


[root@localhost shell]# awk 'BEGIN {print "length($1)",length($1)} {print "length

($1):"length("a"$1)}' a.txt

[root@localhost shell]# awk 'BEGIN {print split("123#456#678", arr, "#"),arr[0], arr[1], 

arr[2]}'

[root@localhost shell]# echo "65" | awk '{printf"%c\n",$0}';

[root@localhost shell]# awk '{printf "%-15s %s\n", $1,$3}' a.txt

[root@localhost shell]# who | awk '{print $1" you are connect "$2}'


awk文件
#!/bin/awk -f

BEGIN{
        print("=========== Begin =================");
        strRecord="123#456#789";
        split(strRecord, arr, "#");

        FS="\t";
};

{
        print $0;
        total += $6;
}

END{
        for (i in arr)
        {
                print(arr[i]"\t");
        }
        print("total val:", total);
        print("******* end of awk ***********");
};
 

相关推荐