我有兩個文本文件。 hash_only.txt和final_output.txt hash_only.txt如下所示。使用bash處理文件和文本
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我想從「final_output.txt」的文件中提取柱2,3的值如193548,401125等,其中第1點的匹配193548,401125等,並輸出 第2列,3到print_193548,print_401125等。
我該怎麼做。在上面的代碼中,我需要在部分代碼裏面放一些代碼。但是我無法弄清楚,因爲我不是很精通bash。
編輯:
我現在已經修改了我的腳本看起來likefor我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] *的文件。我不明白爲什麼不可以?
所以你想創建一堆文件,對吧?第一個文件中的每個不同值都有一個值? –
是的,這正是我想要的。 – liv2hak
'gawk'命令將完成所有工作。 'if ... else' /'for ... loop'可以被刪除。 – kev