Home > Linux > Main text

Usage of awk command


Tag: grep, awk, sed


Basic usage


check common elements in two files of specific fields

awk 'NR==FNR{a[$1];next} $1 in a{print $1}' file1 file2

split string into a array, return length of the array

awk 'BEGIN{info="it is a test";lens=split(info,tA," ");print length(tA),lens;}'
4 4

by default awk array is not sorted, use asort

awk 'BEGIN{info="it is a test";split(info,tA," ");for(k in tA){print k,tA[k];}}'
4 test
1 it
2 is
3 a

# sort by element in alphabeta
awk 'BEGIN{info="it is a test";split(info,tA," ");asort(tA);for(k in tA){print k,tA[k];}}'
1 a
2 is
3 it
4 test

check element in a array

awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if( "c" in tB){print "ok";};for(k in tB){print k,tB[k];}}' 
a a1
b b1

delete element in an array

awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";delete tB["a"];for(k in tB){print k,tB[k];}}' 
b b1

swap two variables

source: stackoverflow

awk ' { t = $1; $1 = $2; $2 = t; print; } ' input_file

set output filed separator

source: stackoverflow

# swap two columns, also keep tab as separator
awk 'BEGIN{FS="\t";OFS="\t";}{if($2>$3){t=$2;$2=$3;$3=t};if($5>$6){t=$5;$5=$6;$6=t;};print}' HEK293T.bedpe > HEK293T.sort.bedpe

source: stackexchange

awk '{print $NF}' inputfile

take absolute value

source: Unix & Linux

# https://unix.stackexchange.com/questions/220588/how-to-take-the-absolute-value-using-awk

# define function: abs
$ echo "2 3"|awk '{print $1-$2}'
-1
$ echo "2 3"|awk 'function abs(v) {return v < 0 ? -v : v} {print abs($1-$2)}'
1

# round way: call sqrt function
$ echo "2 3"|awk '{print sqrt(($1-$2)^2)}'
1

将某个文件按照另一个文件的某一列进行排序

参考这里

# 读取第2个文件,$1作为key,整行$0作为value值存储下来
# 读取第1个文件,因为每个$1都存在X2里面,所以判断直接打印
# 打印的是原先存储的值,也就是要排序的第2个文件
# 因而可以完成按照文件1对文件2进行排序
awk 'FNR==NR {x2[$1] = $0; next} $1 in x2 {print x2[$1]}' second first

If you link this blog, please refer to this page, thanks!
Post link:https://tsinghua-gongjing.github.io/posts/awk.html

Previous: Distribution/hist plot