awk使用小记

awk命令使用基本格式: awk ‘条件1{操作1}条件2{操作2}…’ [文件名]。
1.基本变量:
NF:每一行($0)的总的字段数
NR:当前行数
FS:当前分隔符,默认是空格
BEGIN:开始
END:结束
注意,如果要使用shell的变量,用:”‘$variable'”。

附处理sigma.log*的脚本:

for OUTPUT in $(ls sigma.log*)
do
if [ $OUTPUT != "sigma.log" ]; then
awk 'BEGIN{ORS="";print "'$OUTPUT'","\t"}
NR==64{print 56,$10," "}
NR==65{print 57,$10,"\n"}' $OUTPUT |
sed 's/sigma.log/ /g'
fi
done |
sort -k1 -n

2.如果要在AWK中使用shell里的变量,可以用:”‘$变量名'”。

比如从一些输出文件”test-run-nprocs-proc.out”提取数据的脚本,在awk中用”‘$nprocs'”使用for循环的变量nprocs。


for nprocs in 4 6 8 12 16
do
filename="test-run-"
filename+=$nprocs
filename+="proc.out"
# echo $filename
echo " "
grep 'timing' $filename | awk 'NR%4==2 { serialtime = $6 }
NR%4==3 { size = $10; paratime = $6}
NR%4==0 { precondtime = $9;
awknproc="'$nprocs'";
printf "%s %10d %12.7f %12.7f %12.7f \n",awknproc,size,serialtime,paratime,precondtime }'
done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s