awk应用举例

Posted by 4Aiur on 04/06/2010 in Shell |

打印磁盘INODE最大值

# df -Pi | awk 'BEGIN{OFS="\n";Max=0} {if (NR!=1) {sub(/%/,"",$5); Max=$5>Max?$5:Max}} END{print Max}'

38

打印磁盘空间最大值

df -P | awk 'BEGIN{OFS="\n";Max=0} {if (NR!=1) {sub(/%/,"",$5); Max=$5>Max?$5:Max}} END{print "diskUsedSpacePercent: "Max,"diskSpaceUpdateTime: "systime()}'

diskUsedSpacePercent: 55
diskSpaceUpdateTime: 1252898042

按netstat中ESTABLISHED状态的连接数量进行排序

netstat -an 2>/dev/null | awk /ESTABLISHED/'{print $5}' | awk -F: '{!ip[$(NF-1)]++} END { for (item in ip) print item,ip[item] | "sort -k2 -rn"}' | more

打印系统连接数

echo -e "\\033[1;32m"awk /Tcp/'{print $10}' /proc/net/snmp "\\033[0;39m"

CurrEstab 31

The Split function

#!/usr/bin/awk -f
BEGIN {
# this script breaks up the sentence into words, using 
# a space as the character separating the words
    string="This is a string, is it not?";
    search=" ";
    n=split(string,array,search);
    for (i=1;i

Word[1]=This
Word[2]=is
Word[3]=a
Word[4]=string,
Word[5]=is
Word[6]=it
Word[7]=not?

Tags: ,

Copyright © 2010-2024 4Aiur All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.