2014年6月14日 星期六

Awk Samples

1.

awk -v COLUMN=2 '{ sum += $COLUMN } END { print sum}'

set 2nd to COLUMN and sum ,
after process all lines , print the sum

2.

awk -F"[()s]" -v Q=3 '{qSum += $Q} END { print qSum}'

set separater to  ( , ) , s

set 3nd to Q and sum it,
after process all lines , print the sum

3.

awk -F"[()s]" '{qSum += $3; lSum += $7; rSum += $11} END { print qSum,lSum,rSum}'
set separater to  ( , ) , s
sum 3 fileds
print them


4.

#!/usr/bin/awk -f
NR%4 == 0{
        print ARGC
        for ( k=0 ; k < ARGC ; k++) {
                print "ARGV[" k "] = [" ARGV[k] "]"
        }
}



5. regular expression


EX1 :

test.awk
#!/usr/bin/gawk -f
$0 ~ /Time:/{
        #print $0;
        match($0, /Time: ([0-9]+)/, arr)
        if( arr[1] != "")
                print arr[1];
#               print NR;
}

Search String "Time:"

Match the   Time: 140613 22:26:16   to arr
print arr[1]

Output
140613
140613
140613

#!/usr/bin/gawk -f
$0 ~ /Time: [0-9]+ [0-9][0-9]:[0-9][0-9]/{
        print $0
}

Seach String " Time:  [d]+ dd:dd:dd"

Output :

# Time: 140613 22:26:16
# Time: 140613 22:26:17
# Time: 140613 22:26:18
# Time: 140613 22:26:19
# Time: 140613 22:26:20
# Time: 140613 22:26:21

沒有留言:

張貼留言