使用bash进行文件和文本处理

使用bash进行文件和文本处理,bash,shell,sed,awk,Bash,Shell,Sed,Awk,我有两个文本文件。hash_only.txt和final_output.txt hash_only.txt如下所示 193548 401125 401275 193548 1199687744 5698758206701808640 193548 1216464960 5698758206761818112 193548 1216464960 5698758206778417152 193548 4236691520 56987582067789

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

193548
401125
401275
193548      1199687744  5698758206701808640
193548      1216464960  5698758206761818112
193548      1216464960  5698758206778417152
193548      4236691520  5698758206778945280
401125      2138607488  5698762375908890880
401125       863932288  5698762375909423360
401125      3884158848  5698762375910044160
401125      2609483648  5698762375911032320
final_output.txt如下所示

193548
401125
401275
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中的所有值,如193548401125等,我想从文件'final_output.txt'中提取第2,3列,其中第1列与193548401125等和输出匹配 第2列、第3列打印出193548号、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\u0-9]*的文件。我不明白为什么不创建

awk '
FNR==NR {
    hash[$1]
    next
}
$1 in hash {
    printf("%s\t%s\n", $2, $3) > "print_"$1;
}' hash_only.txt final_output.txt
多么神奇,我的解决方案几乎与彼得的完全相同。

试试这个:

nawk 'FNR==NR{a[$0];next}($1 in a){print $2,$3>$1}' hash_only.txt  final_output.txt 
这将实际创建一个名为第一个字段的文件,并按照您请求的方式存储输出

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

所以你想创建一堆文件,对吗?第一个文件中的每个不同值对应一个?是的。这正是我想要的。所有工作都将由
gawk
命令完成<代码>if…else
/
for…loop
可以删除。您是否建议我在代码中的if-then部分之后添加此代码?我尝试过,但似乎不起作用。它只是打印出一系列值。复制并粘贴到您的终端。它将在当前目录中创建两个文件(
print\u 193548
print\u 401125
)。非常感谢。它起作用了。但我想调整$2和$3之间的间距,以便我可以将其作为gnuplot的输入。我尝试过的任何想法“\t'”等似乎都不起作用?可以省略括号。
$ cat ./print_401125
 2138607488 5698762375908890880
 863932288 5698762375909423360
 3884158848 5698762375910044160
 2609483648 5698762375911032320