I have a folder (Transactions) with hundreds of .csv files. I want to search through all csv files for a particular string and get the output of each line that contains that string. Preferably output to a txt file. For example, let's say I want to find any record that contains the name John Doe. In windows I would use the following:

FINDSTR /n /i "John.Doe" *.csv >> johndoe.txt
  • /n to give me the line number
  • /i to ignore case
  • "." to separate words
  • *.csv to search all files that end with .csv, and
  • >> to output to a new file which contains all data that includes the string (into the same folder).

I'm looking for a similar command to use in Ubuntu Terminal that will search through all .csv files and output any hits to a txt file. I've looked into grep and awk but don't know how to use them. Any help would be appreciated. Thanks.

  • You can use sed, grep, awk, perl and likely other tools as well. Give a more specific example of your input file and desired output. – Panther Sep 17 '15 at 22:04

I've looked into grep and awk but don't know how to use them.

The grep documentation has plenty of examples. The first one in the Usage section demonstrates case-insensitive searching:

Here is an example command that invokes GNU grep:

 grep -i 'hello.*world' menu.h main.c

... The -i option causes grep to ignore case, causing it to match the line ‘Hello, world!’, which it would not otherwise match.

And if I search for line number in the aforementioned page:

Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)

>> foo and *.csv are handled by the shell (probably how you'd expect). So:

grep -in "John.Doe" *.csv >> johndoe.txt

Options like /i, /n, etc. usually begin with hyphens in the Unix world.


The free application 'CsvFileSearch' will do exactly what you want.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.