Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created August 9, 2011 09:59
Show Gist options
  • Save kencoba/1133695 to your computer and use it in GitHub Desktop.
Save kencoba/1133695 to your computer and use it in GitHub Desktop.
awk-like utility functions for clojure.
(use '[clojure.contrib.duck-streams :only (write-lines writer read-lines)])
(use '[clojure.contrib.str-utils :only (re-split)])
(in-ns 'clojure.contrib.duck-streams)
(def *default-encoding* "Shift_JIS")
(in-ns 'user)
(defmacro awk-print [input-file f]
`(map ~f (for [line# (read-lines ~input-file)] line#)))
(defmacro awk [input-file output-file f]
`(write-lines (writer ~output-file)
(awk-print ~input-file ~f)))
(defn str-convert-encode [encoding & strs]
{:pre [(string? encoding)]
:post [(string? %)]}
(String. (.getBytes (apply str strs) encoding)))
(def to-utf8 (partial str-convert-encode "UTF-8"))
(def to-euc (partial str-convert-encode "EUC-JP"))
(def to-sjis (partial str-convert-encode "Shift_JIS"))
(import [java.io File])
(defn dir-descendants
"指定されたディレクトリ以下のファイルのフルパス一覧を取得する"
[dir]
(let [children (.listFiles (File. dir))]
(lazy-cat
(map (memfn getPath) (filter (memfn isFile) children))
(mapcat dir-descendants
(map (memfn getPath) (filter (memfn isDirectory) children))))))
(defn re-filter
"コレクションから、正規表現に部分一致する要素を取り出す"
[re coll]
(filter #(not (nil? (re-find re %))) coll))
(defn find-by-name
"指定されたディレクトリ内のファイルのうち、
ファイル名が正規表現に部分一致するファイル名の一覧を取り出す"
[re directory]
(re-filter re (dir-descendants directory)))
;(for [file (find-by-name #"txt" "d:\\home\\ken")]
; (awk-print file
; #(if (not (nil? (re-find #"con" %))) "<aaaaa>" %)))
(defn re-find?
"文字列が正規表現にマッチしていればtrueを返す"
[re s]
(if (nil? (re-find re s)) false true))
;(filter #(not (nil? %))
; (flatten (for [file (find-by-name #"txt" "d:\\home\\ken")]
; (awk-print file #(if (re-find? #" - " %) %)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment