awk usage examples
时间:2009-08-19 来源:creatory
Usage:awk '{pattern+action}' {filenames}
When awk read content,the all line was allocated to $0,eacho field devided by blanks or tabs was allocated $1,$2,$3...
So,the two following commands are equal:
awk '{print $0}' filename
awk '{print $1,$2,$3,$4,$5}' filename
We can only get two line by any order:
awk '{print $3,$0}' filename
/pattern/ usage:
awk '/Bill/{print $0}' filename
multi-pattern usage:
/pattern1|pattern2/
awk '/Bill|Gade/{print $0}' filename
change line:
awk '/AL/{print $1;print $2}' filename
multi-condition:
awk '/AL/{print $1} {print $2}' filename
the condition is only available at {print $1},{print $2} is non-condition.
Add extrnal character:
awk '/AL/{print $1,$2;print $4", "$5"\n"}' filename
awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5}' filename
other sperator:
awk -F ":" 'print $0' filename(awk '{FS=":"}{print $0}' filename)
awk -F ":" '{OFS="-"}{print $0}' filename
awk '{print NR,$0}' filename
awk -F ":" '/4601[2-5]/{print $0}' filename
awk '{print $1":"$2":"$3":"$4":"$5}' filename>newfilename
awk '{print $1,"QTY:" $2,"PRICE:"$3,"TOTAL:"$2*$3}' inventory
awk '{x=x+$2}{print x}' inventory
awk 'BEGIN{print "Item Quartity Price Total"}{x=x+($2+$3)}END{print "Total value of inventory:"x}' inventory
//get qq program pid
ps aux | grep qq | awk '{print $2}'
When awk read content,the all line was allocated to $0,eacho field devided by blanks or tabs was allocated $1,$2,$3...
So,the two following commands are equal:
awk '{print $0}' filename
awk '{print $1,$2,$3,$4,$5}' filename
We can only get two line by any order:
awk '{print $3,$0}' filename
/pattern/ usage:
awk '/Bill/{print $0}' filename
multi-pattern usage:
/pattern1|pattern2/
awk '/Bill|Gade/{print $0}' filename
change line:
awk '/AL/{print $1;print $2}' filename
multi-condition:
awk '/AL/{print $1} {print $2}' filename
the condition is only available at {print $1},{print $2} is non-condition.
Add extrnal character:
awk '/AL/{print $1,$2;print $4", "$5"\n"}' filename
awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5}' filename
other sperator:
awk -F ":" 'print $0' filename(awk '{FS=":"}{print $0}' filename)
awk -F ":" '{OFS="-"}{print $0}' filename
awk '{print NR,$0}' filename
awk -F ":" '/4601[2-5]/{print $0}' filename
awk '{print $1":"$2":"$3":"$4":"$5}' filename>newfilename
awk '{print $1,"QTY:" $2,"PRICE:"$3,"TOTAL:"$2*$3}' inventory
awk '{x=x+$2}{print x}' inventory
awk 'BEGIN{print "Item Quartity Price Total"}{x=x+($2+$3)}END{print "Total value of inventory:"x}' inventory
//get qq program pid
ps aux | grep qq | awk '{print $2}'
相关阅读 更多 +
排行榜 更多 +