awk

1.awk common format and example

 awk '{cmd}' inputfile

or awk 'condition {cmd}'

or awk '{if(condition) cmd}

for example, b.txt

a b c
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

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"

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值