Skip to content

Instantly share code, notes, and snippets.

@ponkore
Created April 12, 2012 04:09
Show Gist options
  • Save ponkore/2364544 to your computer and use it in GitHub Desktop.
Save ponkore/2364544 to your computer and use it in GitHub Desktop.
Cheap grep by Clojure
;;;
;;; 簡単 grep。のつもりが、あんまりかっこ良くないorz
;;;
(use '[clojure.java.io :as io])
(defn indexed [coll]
(map vector (iterate inc 1) coll))
(defn grep [re file]
(with-open [rdr (io/reader file)]
(doall
(map #(printf "%d: %s\n" (first %) (nth % 1))
(filter #(re-find (re-pattern re) (nth % 1)) (indexed (line-seq rdr)))))))
user=> (grep "[Ss]ervice" "/etc/passwd")
13: daemon:*:1:1:System Services:/var/root:/usr/bin/false
15: _networkd:*:24:24:Network Services:/var/empty:/usr/bin/false
17: _lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
19: _scsd:*:31:31:Service Configuration Service:/var/empty:/usr/bin/false
20: _ces:*:32:32:Certificate Enrollment Service:/var/empty:/usr/bin/false
56: _softwareupdate:*:200:200:Software Update Service:/var/empty:/usr/bin/false
69: _kadmin_admin:*:218:-2:Kerberos Admin Service:/var/empty:/usr/bin/false
70: _kadmin_changepw:*:219:-2:Kerberos Change Password Service:/var/empty:/usr/bin/false
80: _krb_kadmin:*:231:-2:Open Directory Kerberos Admin Service:/var/empty:/usr/bin/false
81: _krb_changepw:*:232:-2:Open Directory Kerberos Change Password Service:/var/empty:/usr/bin/false
(nil nil nil nil nil nil nil nil nil nil)
user=>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment