{"id":20443,"date":"2022-11-09T17:34:56","date_gmt":"2022-11-09T12:04:56","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/"},"modified":"2022-11-09T17:34:56","modified_gmt":"2022-11-09T12:04:56","slug":"solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/","title":{"rendered":"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-53198522\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"53198522\" data-parentid=\"53198257\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>A simple function to extract the Nth column from your text makes this reasonably straight-forward.  I&#8217;ve assumed when you say &#8220;Column 11&#8221; you mean, the 11 column counting from 1, not the index-11 column where the 1st item is index-0<\/p>\n<p>Pseudo-Code:<\/p>\n<pre><code>Until there's no data left ~\n    Read line1 from file1\n    Read line2 from file2\n    Extract Col11 from line1 as a real number\n    Extract Col2 &amp; Col3 from line2 as real numbers\n    IF Col11 is within Col2 &amp; Col3\n        do something\n<\/code><\/pre>\n<p>Python Code:<\/p>\n<pre><code>import sys\n\n# Given a space-separated row of data, return the Nth column as a real number\ndef getNthColumn(row, N):\n    # Single-space the row, removing tabs, double-spaces etc.\n    row = ' '.join(row.split())\n    fields = row.split(' ')\n    result = float(fields[N-1])   # fields are numbered 0-&gt;(N-1)\n    #print(\"Returning column %d from [%s] -&gt; %f\" % (N, row, result))\n    return result\n\nif (len(sys.argv) == 3):\n    fin1 = open(sys.argv[1], \"rt\")\n    fin2 = open(sys.argv[2], \"rt\")  #TODO - handle file-not-found errors, etc.\n\n    line1 = fin1.readline()\n    line2 = fin2.readline()\n    while (line1 != \"\" and line2 != \"\"):\n        # Get the columns from the two lines\n        f1_col11 = getNthColumn(line1, 11)\n        f2_col2  = getNthColumn(line2,  2)\n        f2_col3  = getNthColumn(line2,  3)  ### TODO handle errors\n        # work out if it's a keeper\n        # print(\"Is %f &gt;= %f and %f &lt;= %f\" % (f1_col11, f2_col2, f1_col11, f2_col3))\n        if (f1_col11 &gt;= f2_col2 and f1_col11 &lt;= f2_col3):\n            print(\"MATCH: \"+line1)\n        else:\n            print(\"NO-MATCH: \"+line1)\n        # Next rows\n        line1 = fin1.readline()\n        line2 = fin2.readline()\nelse:\n    print(\"Give 2 files as arguments\")\n<\/code><\/pre>\n<p>To be honest, if speed really is critical for this, it would be better to write it in a compiled language, e.g.: C\/C++\/Pascal, etc. etc.<\/p>\n<p>EDIT: tested and working, added some debug print()s<\/p>\n<p>EDIT2: Search file1-row against all rows in file2<\/p>\n<pre><code>import sys\n\n# Hold all the file2 Columns\nfile2_col23 = []\n\n# Given a space-separated row of data, return the Nth column as a real number\ndef getNthColumn(row, N):\n    # Single-space the row, removing tabs, double-spaces etc.\n    row = ' '.join(row.split())\n    fields = row.split(' ')\n    try:\n        result = float(fields[N-1])   # fields are numbered 0-&gt;(N-1)\n    except:\n        sys.stderr.write(\"Failed to fetch number column %d from [%s]\" % (N, row))\n        sys.exit(1)\n    #print(\"Returning column %d from [%s] -&gt; %f\" % (N, row, result))\n    return result\n\nif (len(sys.argv) == 3):\n    fin1 = open(sys.argv[1], \"rt\")\n    fin2 = open(sys.argv[2], \"rt\")  #TODO - handle file-not-found errors, etc.\n\n    # Load in the whole of file2, but just the column2 &amp; column3\n    # note the minimum col2 and maximum c3\n    line2 = fin2.readline()\n    min_c2 = None\n    max_c3 = None\n    while (line2 != \"\"):\n        col2 = getNthColumn(line2, 2)\n        col3 = getNthColumn(line2, 3)\n        file2_col23.append( ( col2, col3 ) )\n        # Note the min c2 and max c3 so we can quickly know if a search can\n        # possible produce a result\n        if (min_c2 == None or col2 &lt; min_c2):\n            min_c2 = col2\n        if (max_c3 == None or col3 &gt; max_c3):\n            max_c3 = col3\n        # next line\n        line2 = fin2.readline().strip()\n\n    # sort the columns to allow us to short-cut searching\n    file2_col23.sort()\n\n\n    line1 = fin1.readline()\n    while (line1 != \"\"):\n        col11 = getNthColumn(line1, 11)\n\n        matched = False\n        # is col11 is within any file2 row col2 or col3\n        if (col11 &gt;= min_c2 and col11 &lt;= max_c3):   # make sure the search is worthwhile\n            for col23 in file2_col23:\n                (col2, col3) = col23\n                if (col11 &gt;= col2 and col11 &lt;= col3):\n                    matched = True\n                    break\n\n        if (matched == True):\n            print(\"MATCH: \"+str(line1))\n        else:\n            print(\"NO-MATCH: \"+str(line1))\n\n        # Next row\n        line1 = fin1.readline()\nelse:\n    print(\"Give 2 files as arguments\")\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">9<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] A simple function to extract the Nth column from your text makes this reasonably straight-forward. I&#8217;ve assumed when you say &#8220;Column 11&#8221; you mean, the 11 column counting from 1, not the index-11 column where the 1st item is index-0 Pseudo-Code: Until there&#8217;s no data left ~ Read line1 from file1 Read line2 from &#8230; <a title=\"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\" aria-label=\"More on [Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[349],"class_list":["post-20443","post","type-post","status-publish","format-standard","hentry","category-solved","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] A simple function to extract the Nth column from your text makes this reasonably straight-forward. I&#8217;ve assumed when you say &#8220;Column 11&#8221; you mean, the 11 column counting from 1, not the index-11 column where the 1st item is index-0 Pseudo-Code: Until there&#039;s no data left ~ Read line1 from file1 Read line2 from ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-09T12:04:56+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches\",\"datePublished\":\"2022-11-09T12:04:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\"},\"wordCount\":131,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"python\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\",\"name\":\"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-09T12:04:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches - JassWeb","og_description":"[ad_1] A simple function to extract the Nth column from your text makes this reasonably straight-forward. I&#8217;ve assumed when you say &#8220;Column 11&#8221; you mean, the 11 column counting from 1, not the index-11 column where the 1st item is index-0 Pseudo-Code: Until there's no data left ~ Read line1 from file1 Read line2 from ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/","og_site_name":"JassWeb","article_published_time":"2022-11-09T12:04:56+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches","datePublished":"2022-11-09T12:04:56+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/"},"wordCount":131,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["python"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/","url":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/","name":"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-09T12:04:56+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-extract-rows-having-the-11th-column-values-lies-between-2nd-and-3nd-of-a-second-file-if-1st-column-matches\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Extract rows having the 11th column values lies between 2nd and 3nd of a second file if 1st column matches"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20443"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20443\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}