AWK

1.awk common format and example

 awk '{cmd}' inputfile

or awk 'condition {cmd}'

or awk '{if(condition) cmd}

for example, b.txt

  1. a b c  
  2. d e f  


>>awk '{printf $1}' b.txt

a

d

>>awk '$1=="a" {print $2,$3}' b.txt

b c

>>awk  '{if($1=="a") print $2}' b.txt

b

 

notice:

a. all awk  script command in ''

b. awk command in {}

c. string in ""

 

2. variable in awk

a. OFS, output field separator 

b. ORS,  output record separator

c. FS field separator

d. RS record separator

e. FNR full number record

f. NR current number of record

g. NF number of field

h ARGC like c's argc

i ARGV like c's argv

j FILENAME filename

$0 like shell's $0,

$1,$2...$n the data row of n

for example

awk  {'$1=="a" print $2}' b.txt

 

3.BEGIN, END

do some initialization or conclusion 

for example

  1. awk 'BEGIN{num=0;}{if($1=="a") {num=num+1}} END{print num}' b.txt  


count the number of the line who's $1 is "a"

  1. awk '{if ($5>max) max=$5} END{print max}'  

get the biggest of $5, "()" can't be omitted.

4. write awk in to an awk file and execute it

awk -f cmd.awk data.awk

 

5.function

function div (a,b)

{return a/b;}

the first "{" can  be same line with function or another.

unlike BEGIN or END which must be in one line.

 

6. with regular expression

ls -al | awk '$5 ~/^6/ {print $0}' 

"!~" means doesn't match

ls -al | awk 'if ($5 ~/^6/) {print $0}' 

 

7. change FS

awk -F [,;] '//{}'

means use "," and ";" as separator 

 

8. control output format

awk 'BEGIN {printf "%d\n", FNR}'

 

9, still use  "||" and "&&"

$num,  stands $0,$1.. when num=1,2

 

10. awk string function

gsub(r,s)在整个$0中用s替代r
gsub(r,s,t)在整个t中用s替代r
index(s,t)返回s中字符串t的第一位置
length(s)返回s长度
match(s,r)测试s是否包含匹配r的字符串
split(s,a,fs)在fs上将s分成序列a
sprint(fmt,exp)返回经fmt格式化后的exp
sub(r,s)用$0中最左边最长的子串代替s
substr(s,p)返回字符串s中从p开始的后缀部分
substr(s,p,n)返回字符串s中从p开始长度为n的后缀部分

 

11.example

awk 'BEGIN{pos=0;find=0} {if(find==0){while($pos!="pc"){pos=pos+1;if(pos<NF){
find=1}}}{print substr($(pos+2),index($(pos+2),"lib/")+4),$(pos+1)}}' input.txt | xargs addr2line -e 2>&1 | tee stack.txt

posted @ 2012-04-05 22:01  cascais  阅读(320)  评论(0编辑  收藏  举报