[英]Looping each row in file1.txt over all rows of file2.txt for comparing
I have two text files, say file1.txt contains something like我有两个文本文件,说 file1.txt 包含类似
100.145 10.0728 100.145 10.0728
100.298 10.04 100.298 10.04
and file2.txt contains something like和 file2.txt 包含类似
100.223 8.92739 100.223 8.92739
100.209 9.04269 100.209 9.04269
100.084 9.08411 100.084 9.08411
100.023 9.01252 100.023 9.01252
I want to compare column 1 and column 2 of both files and print match if the difference of both columns in file1.txt and in file2.txt is less or equal to 0.001.我想比较两个文件的第 1 列和第 2 列,如果 file1.txt 和 file2.txt 中两列的差异小于或等于 0.001,则打印匹配。
since both files don't have equal no.因为两个文件的编号不相等。 or rows, i want row1 of file1.txt to be compared with all the rows of file2.txt, then it will now pick row2 of file1.txt and do same until all rows of file1.txt are exhausted.
或行,我希望将 file1.txt 的 row1 与 file2.txt 的所有行进行比较,然后它现在将选择 file1.txt 的 row2 并执行相同操作,直到 file1.txt 的所有行都用完为止。 Difference should like this ($1 file1.txt - $1 file2.txt) and ($2 file1.txt - $2 file2.txt) if difference of both is less or equal 0.001, it should print the rows in both file that match
差异应该是这样的 ($1 file1.txt - $1 file2.txt) 和 ($2 file1.txt - $2 file2.txt) 如果两者的差异小于或等于 0.001,它应该打印两个文件中匹配的行
you can try this;你可以试试这个;
#!/bin/bash
while read line; do
while read line2; do
Col1F1=$(echo $line | awk '{print $1}')
Col1F2=$(echo $line2 | awk '{print $1}')
Col2F1=$(echo $line | awk '{print $2}')
Col2F2=$(echo $line2 | awk '{print $2}')
if [ ! -z "${Col1F1}" ] && [ ! -z "${Col1F2}" ]; then
diffCol1=$(awk '{print $1-$2}' <<< "$Col1F1 $Col1F2")
diffCol2=$(awk '{print $1-$2}' <<< "$Col2F1 $Col2F2")
if (( $(echo "$diffCol1 < 0.001" |bc -l) && $(echo "$diffCol2 < 0.001" |bc -l))); then
echo -e ${Col1F1} "\t" ${Col2F1} "\t" ${Col1F2} "\t" ${Col2F2} "\n"
fi
fi
done < file2.txt
done < file1.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.