Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Last active July 4, 2018 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuchunyang/3f9b4610c4cf6aff4647dc3ccafbdc26 to your computer and use it in GitHub Desktop.
Save xuchunyang/3f9b4610c4cf6aff4647dc3ccafbdc26 to your computer and use it in GitHub Desktop.
Print matched lines
;;; grep.rkt --- Print matched lines
;; Author: Xu Chunyang <mail@xuchunyang.me>
;; 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 <mail@xuchunyang.me>
;; ~/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