Comparing two columns in different file and printing match if difference between record in a columns less than or equal to 0.001
<p>I have two text files, say file1.txt contains something like </p>

<pre><code>100.145 10.0728
100.298 10.04
100.212 10.0286
</code></pre>

<p>and file2.txt contains something like </p>

<pre><code>100.223 8.92739
100.209 9.04269
100.084 9.08411
</code></pre>

<p>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.</p>

<p>Just read both files, split by line break, split those lines by spaces and then loop over the first files lines and for each line check whether the second files line at this position matches your condition.</p>

<pre class="lang-py prettyprint-override"><code>with open("file1.txt", "r") as f:
  f1_content = f.read()

with open("file2.txt", "r") as f:
  f2_content = f.read()

f1_lines = [line.split() for line in f1_content.split("\n")]
f2_lines = [line.split() for line in f2_content.split("\n")]

for i, line in enumerate(f1_lines):
  if abs(float(line[0]) - float(f2_lines[i][0])) &lt;= 0.001 and abs(float(line[1]) - float(f2_lines[i][1])) &lt;= 0.001:
    print("Match at line {0}".format(i))
</code></pre>

<p>Solution using <code>zip</code> as proposed by @BrianCain:</p>

<pre class="lang-py prettyprint-override"><code>with open("file1.txt", "r") as f:
  f1_content = f.read()

with open("file2.txt", "r") as f:
  f2_content = f.read()

f1_lines = [line.split() for line in f1_content.split("\n") if line != ""]
f2_lines = [line.split() for line in f2_content.split("\n") if line != ""]

for line in zip(f1_lines, f2_lines):
  if abs(float(line[0][0]) - float(line[1][0])) &lt;= 0.001 and abs(float(line[0][1]) - float(f2_lines[1][1])) &lt;= 0.001:
    print("Match at line {0}".format(line))
</code></pre>

<p><strong>Requested Explanation</strong> of the solution using <code>zip</code>:
First we open both file (using with, as this closes the file after the block) and save the content to some variables. We use <code>read()</code> instead of <code>readlines()</code> bacause the latter doesnt remove an <code>\n</code> when splitting at linebreaks. So i usually just use read() and split by <code>\n</code>, as this does the job.</p>

<p>We then create ourselves for each file a list of tuples. Each entry in the list represents one line, each tuple (which is one of those entries) contains one entry per column. This is done using <a href="http://www.python-course.eu/list_comprehension.php" rel="nofollow">list comprehensions</a>:</p>

<pre class="lang-py prettyprint-override"><code>f1_lines = [line.split() for line in f1_content.split("\n") if line != ""]
</code></pre>

<p>now we can iterate over the lines. <code>zip</code> is pretty useful for that, as it can combine two lists of equal length to one list, containing tuples of the entries of the lists at the same index. Example:</p>

<pre class="lang-py prettyprint-override"><code>zip([1, 2, 3], [4, 5, 6])
</code></pre>

<p>would produce</p>

<pre class="lang-py prettyprint-override"><code>[(1, 4), (2, 5), 3, 6)]
</code></pre>

<p>For more look here: <a href="https://docs.python.org/3/library/functions.html#zip" rel="nofollow">zip</a></p>

<p>So now we have a list, containing tuples for the lines with an entry for each file. Each entry itself contains two entries for the columns:</p>

<pre class="lang-py prettyprint-override"><code>[(['100.145', '10.0728'], ['100.223', '8.92739']),
 (['100.298', '10.04'], ['100.2985', '10.04001']),
 (['100.212', '10.0286'], ['100.084', '9.08411']),
 (['100.212', '100.2125'], ['100.084', '100.0845'])]
</code></pre>

<p>if we iterate over this list we can access files via the first [x] as in line[0] and columns via the second [y] as in line<a href="http://www.python-course.eu/list_comprehension.php" rel="nofollow">0</a> (accessing the first file at line at the second column).
Now we simply subtract at each line the second files first column value from the first files first column value, take the absolute value (to only get positive floats) and check if its less than or equal to 0.001. The same is done for the second column, and if both are true (or one, if you use OR) then we print our message.</p>

python awk