首页 > 解决方案 > awk 检查列并获取结果

问题描述

给定第一列(文件线程)的每个数量的 2 个文件 pri_seq 和 stb_seq 我必须分别在 ps 和 ss 中评估第二列,然后在 awk 工作的脚本之外产生差异,但在脚本内部却没有,好像它需要逃脱

cat pri_seq
         1                656369
         2                859638

cat stb_seq
         1                625743
         2                817088

cat threads
1
2


cat test.sh
#!/bin/sh

awk '{print $1}' pri_seq > threads

cat threads | while read line; do
        ps=` awk '$1==$line {print $2}' pri_seq`;
        ss=` awk '$1==$line {print $2}' stb_seq`;
        echo $ps
        echo $ss
#       ((diff=$ps-$ss))
        echo "Thread $line Sequence Primary= $ps  Sequence Standby = $ss  Difference = $diff"
done
  

它不重视 ps 和 ss

(PROD920) oracle@e812stb:/home/oracle/bin/chk_dg> ./test.sh


Thread 1 Sequence Primary=   Sequence Standby =   Difference =


Thread 2 Sequence Primary=   Sequence Standby =   Difference =


Thread  Sequence Primary=   Sequence Standby =   Difference =


out of the script:

ps=`awk '$1==1 {print $2}' pri_seq`
echo $ps
656369

你有解决方案吗?

标签: awkescaping

解决方案


你的问题不清楚,但这是你想要做的吗?

awk '
    NR==FNR { ps[$1]=$2; next }
    { printf "Thread %d  Sequence Primary= %d  Sequence Standby= %d  Difference= %d\n", $1, ps[$1], $2, ps[$1]-$2 }
' pri_seq stb_seq
Thread 1  Sequence Primary= 656369  Sequence Standby= 625743  Difference= 30626
Thread 2  Sequence Primary= 859638  Sequence Standby= 817088  Difference= 42550

推荐阅读