Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For a solution using <code>awk</code>, see the <a href="https://stackoverflow.com/a/18347009/15168">answer</a> by <a href="https://stackoverflow.com/users/348785/kev">kev</a>.</p> <p>For an improved solution using <code>grep -f</code>, consider using <code>bash</code> <a href="http://www.gnu.org/software/bash/manual/bash.html#Process-Substitution" rel="nofollow noreferrer">process substitution</a>:</p> <pre><code>grep -f &lt;(sed 's/.*/^&amp;,/' file2.txt) file1.csv </code></pre> <p>This uses <code>sed</code> on the lines in <code>file2.txt</code> to put a caret at the start of each line and a comma at the end, so that when treated as a regex by (GNU?) <code>grep</code>, the patterns only match the exact field value at the start of the line. If you don't have <code>bash</code>, you may be able to use:</p> <pre><code>sed 's/.*/^&amp;,/' file2.txt | grep -f - file1.csv </code></pre> <p>However, not all versions of <code>grep</code> read standard input when you specify <code>-f -</code> (the version on Mac OS X doesn't, for example, but GNU <code>grep</code> does).</p> <p>Alternatively, you could use the <code>join</code> command, with appropriate sorts:</p> <pre><code>join -o 1.1,1.2,1.3,1.4 -t, &lt;(sort file1.csv) &lt;(sort file2.txt) </code></pre> <p>If you're confident the files are already sorted, you can simplify that to just:</p> <pre><code>join -o 1.1,1.2,1.3,1.4 -t, file1.csv file2.txt </code></pre> <p>In Perl, you could use:</p> <pre><code>#!/usr/bin/env perl use strict; use warnings; my $file = 0; my %rows; while (&lt;&gt;) { chomp; $rows{$_}++ if ($file == 0); if ($file == 1) { my($id) = split /,/; print "$_\n" if defined $rows{$id}; } } continue { $file = 1 if eof; } </code></pre> <p>There are probably other ways to do it too; for example, you might find a use for modules such as <a href="http://search.cpan.org/perldoc?Text%3A%3ACSV" rel="nofollow noreferrer">Text::CSV</a>.</p> <p>However, this code reads each line. If it is from the first file, then it creates an entry <code>$rows{$_}++</code> to record that the number was seen. Order and repetition don't matter. In the second (and subsequent) files, it splits the first comma separated field out of the line, and checks whether that number was found in the first file; if so, it prints the whole line. The <code>continue</code> block detects when the code reaches EOF on the first file (in particular) and sets <code>$file = 1;</code> when it does. It is isomorphic with the <code>awk</code> solution. This is a little verbose. There is the <code>-a</code> mode (<code>awk</code> mode), but because the two files need to be treated differently, it's a tad tricky to get it to work right.</p> <p>Of these, I think the <code>grep -f</code> solution is probably the neatest as long as <code>file2.txt</code> is not too large (and I'm not sure what the limit would be — but probably surprisingly large).</p> <p>For a general purpose CSV file manipulation tool, consider <a href="http://code.google.com/p/csvfix" rel="nofollow noreferrer">csvfix</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload