Grep-CSV

April 9, 2019

I used the CSV-processing code from the essay on text-file databases, and the regular-expression matcher from a previous exercise, to build this simple grep-csv that reads from standard input and writes to standard output:

(define (grep-csv n regex)
  (for-each-port
    (filter-port read-csv-record
      (lambda (line) (trex regex (list-ref line (- n 1)))))
    (lambda (line) (write-csv-record line))))

Given an input like this:

Charles,Dickens,Great Expectations,1861
Mark,Twain,The Adventures of Tom Sawyer,1876
William,Shakespeare,Julius Caesar,1599
Isaac,Newton,Philosophiae Naturalis Principia Mathematica,1687

We get an output like this:

> (grep-csv 2 "e.*s")
Charles,Dickens,Great Expectations,1861
William,Shakespeare,Julius Caesar,1599

Note that The Adventures of Tom Sawyer and Philosophiae Naturalis Principia Mathematica match the pattern in the title, but those records are not returned because the match is in the wrong field; with grep, they would be returned as unwanted records.

It is handy to have CSV-splitting and regular-expression-matching code available when needed. You can see all of that code and run the program at https://ideone.com/fGHhRb.

Advertisements

Pages: 1 2

One Response to “Grep-CSV”

  1. V said

    Quikie one in Ruby.

    require 'csv'
    
    def grep_csv(csv_str, col, regex_str)
      regex = Regexp.new(regex_str)
      csv_str
        .lines
        .select { |line| CSV.parse_line(line)[col-1] =~ regex }
        .join
    end
    
    puts grep_csv(File.read(ARGV[0]), ARGV[1].to_i, ARGV[2])
    
    

    Given the example.csv file look like this:

    Charles,Dickens,Great Expectations,1861
    Mark,Twain,The Adventures of Tom Sawyer,1876
    William,Shakespeare,Julius Caesar,1599
    Isaac,Newton,Philosophiae Naturalis Principia Mathematica,1687  
    

    When we run the program like so:

    ruby grep-csv.rb example.csv 2 "e.*s"
    

    The output is:

    Charles,Dickens,Great Expectations,1861
    William,Shakespeare,Julius Caesar,1599
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: