0

我有两个文本文件。hash_only.txt 和 final_output.txt hash_only.txt 如下所示。

193548
401125
401275

final_output.txt 如下所示。

193548      1199687744  5698758206701808640
193548      1216464960  5698758206761818112
193548      1216464960  5698758206778417152
193548      4236691520  5698758206778945280
401125      2138607488  5698762375908890880
401125       863932288  5698762375909423360
401125      3884158848  5698762375910044160
401125      2609483648  5698762375911032320

我正在尝试编写一个执行以下操作的循环。

for i in `cat hash_only.txt` ;
do
    for j in `cat final_output.txt` ;
            do
                    if [ $i -eq $j ]
                    then
                            echo $i $j      
                    fi
            done
 done;

对于 hash_only.txt 中的所有值,例如 193548,401125 等,我想从文件 'final_output.txt' 中提取第 2,3 列,其中第 1 列匹配 193548,401125 等,并将第 2,3 列输出到 print_193548、print_401125 等。

我该怎么做。在上面的代码中,我需要在 then 部分中放置一些代码。但我无法弄清楚,因为我对 bash 不是很精通。

编辑:

我现在已经修改了我的脚本,使其看起来像我在cat hash_only.txt

do
        for j in `cat final_output.txt` ;
                do
                        if [ $i -eq $j ]
                        then
                                gawk 'FNR==NR
                                        { hash[$1]  
                                          next 
                                        }
                                       $1 in hash  { 
                                        print $2,$3 >> "print_"$1; 
                                }' hash_only.txt final_output.txt
                        fi
                done
done;

它没有创建任何名为 print_[0-9]* 的文件。我不明白为什么不呢?

4

3 回答 3

2

试试这个:

nawk 'FNR==NR{a[$0];next}($1 in a){print $2,$3>$1}' hash_only.txt  final_output.txt 

这实际上将创建一个名称为第一个字段的文件,并以您请求的方式存储输出。

于 2012-06-14T05:40:48.030 回答
1
awk '
FNR==NR {
    hash[$1]
    next
}
$1 in hash {
    printf("%s\t%s\n", $2, $3) > "print_"$1;
}' hash_only.txt final_output.txt

多么神奇,我的解决方案几乎与彼得的解决方案相同。

于 2012-06-14T05:36:06.223 回答
-1
cat hash_only.txt | while read FNAME; do { cat final_output.txt |grep ${FNAME} |awk '{$1="";}1' > print_${FNAME}; } ; done ; find ./print_* -type f -size 0 -delete
$ ls ./print_??????
./print_193548  ./print_401125
$ cat ./print_193548
 1199687744 5698758206701808640
 1216464960 5698758206761818112
 1216464960 5698758206778417152
 4236691520 5698758206778945280
$ cat ./print_401125
 2138607488 5698762375908890880
 863932288 5698762375909423360
 3884158848 5698762375910044160
 2609483648 5698762375911032320
于 2019-03-26T11:54:40.097 回答