Last active
July 4, 2018 09:24
-
-
Save xuchunyang/3f9b4610c4cf6aff4647dc3ccafbdc26 to your computer and use it in GitHub Desktop.
Print matched lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; grep.rkt --- Print matched lines | |
;; Author: Xu Chunyang <[email protected]> | |
;; Created: Tue, 03 Jul 2018 21:51:49 +0800 | |
;; Updated: Wed, 04 Jul 2018 17:23:58 +0800 | |
;; Homepage: https://gist.github.com/xuchunyang/3f9b4610c4cf6aff4647dc3ccafbdc26 | |
;;; Commentary: | |
;; Example: | |
;; ======= | |
;; | |
;; ~/gist $ emacs --version | racket grep.rkt '[eE]macs' | |
;; GNU Emacs 25.3.1 | |
;; GNU Emacs comes with ABSOLUTELY NO WARRANTY. | |
;; You may redistribute copies of GNU Emacs | |
;; ~/gist $ racket grep.rkt xuchunyang grep.rkt | |
;; ;; Author: Xu Chunyang <[email protected]> | |
;; ~/gist $ | |
;;; Code: | |
#lang racket/base | |
(define (grep pattern input) | |
(for ([line (in-lines input)]) | |
(when (regexp-match pattern line) | |
(displayln line)))) | |
(module+ main | |
(require racket/cmdline) | |
(command-line | |
#:args (pattern [file (current-input-port)]) | |
(if (port? file) | |
(grep pattern file) | |
(call-with-input-file file | |
(lambda (input) | |
(grep pattern input)))))) | |
;;; grep.rkt ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment