{"id":1867,"date":"2023-11-28T12:07:37","date_gmt":"2023-11-28T17:07:37","guid":{"rendered":"https:\/\/www.unliterate.net\/?p=1867"},"modified":"2023-11-28T12:08:08","modified_gmt":"2023-11-28T17:08:08","slug":"today-i-learned-command-script","status":"publish","type":"post","link":"https:\/\/www.unliterate.net\/index.php\/2023\/11\/28\/today-i-learned-command-script\/","title":{"rendered":"Today I learned: command > script"},"content":{"rendered":"\n<p>Had to compare two files at work today. Actually, I had to compare one file to a series of files to see what data exists in both of them. This technically comes down to a LEFT JOIN where we only want left column data when it exists in the right column.<\/p>\n\n\n\n<p>So, in writing a script in PHP it comes down to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nini_set('MEMORY_LIMIT', '256M');\r\nif (!file_exists($argv&#91;1])) { die('file ' . $argv&#91;1] . ' not found'); }\r\nif (!file_exists($argv&#91;2])) { die('file ' . $argv&#91;2] . ' not found'); }\r\n$fp = fopen($argv&#91;1], 'rt');\r\n$lines = &#91;];\r\ndo {\r\n  $line = trim(fgets($fp));\r\n  if (strlen($line) > 0) {\r\n    $lines&#91;] = $line;\r\n  }\r\n} while (!feof($fp));\r\nfclose($fp);\r\n$fp = fopen($argv&#91;2], 'rt');\r\ndo {\r\n  $line = trim(fgets($fp));\r\n  if (strlen($line) > 0) {\r\n    if (in_array($line, $lines)) {\r\n      echo \"$line\\n\";\r\n    }\r\n  }\r\n} while (!feof($fp));\r\nfclose($fp);<\/code><\/pre>\n\n\n\n<p>This script, albeit working like a charm, takes a while with large amounts of records. <\/p>\n\n\n\n<p>After some googling this script isn&#8217;t really necessary if you use grep correctly. You also gain the speed of an executable in one fell swoop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep -Fxf &#91;file1] &#91;file2]<\/code><\/pre>\n\n\n\n<p>Output is exactly the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Had to compare two files at work today. Actually, I had to compare one file to a series of files to see what data exists in both of them. This technically comes down to a LEFT JOIN where we only want left column data when it exists in the right column. So, in writing a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-1867","post","type-post","status-publish","format-standard","hentry","category-geek-instructions"],"_links":{"self":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/1867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/comments?post=1867"}],"version-history":[{"count":2,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/1867\/revisions"}],"predecessor-version":[{"id":1869,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/1867\/revisions\/1869"}],"wp:attachment":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/media?parent=1867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/categories?post=1867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/tags?post=1867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}