简体   繁体   English

将 file1.txt 中的每一行循环到 file2.txt 的所有行上进行比较

[英]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.

相关问题 从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”中读取的两行写入新文件“file2.txt” - Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt" 如何编写 python 从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”读取的两行写入新文件“file2.txt” - How write python to Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt" FileNotFoundError:[Errno 2] 没有这样的文件或目录:'file1.txt' - FileNotFoundError: [Errno 2] No such file or directory: 'file1.txt' 在 python 中循环一个 txt 文件进行数据处理 - Looping over a txt file for data processing in python Python 如何在一个 file.txt 中写入 5 行,在一个新的 file1.txt 中写入另外 3 行,例如 +=1 - Python How to write 5 lines in one file.txt and the other 3 lines in a new file1.txt with +=1 for example 遍历txt文件 - Looping through a txt file 将字符串与 a.txt 文件进行比较 - Comparing a string to a .txt file 如何使txt文件中的每一行成为一个列表? - How to make each row in a txt file a list? 为所有子文件夹中的每个 txt 文件运行一个脚本 - Run a script for each txt file in all subfolders 比较列表和txt文件中的列 - comparing lists to columns in a txt file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM