如何使用 babashka 过滤 tail 的输出

How to filter output of tail with babashka

我想过滤和修改 tail 命令的输出。这是我想出的:

#!/usr/bin/env bb

(ns script
  (:require
   [clojure.java.io :as io]
   [clojure.string :as str]
   ))

(->> (line-seq (io/reader *in*)
               (filter #(re-find #"^\[.*CONSOLE" %))
               (map #(str "carpenter " %)))

它适用于普通尾巴。但我想将它用于“tail -f”命令。 有什么想法吗?

感谢

此示例开始向文件写入两种消息:HELLO 和 BYE。然后它启动一个 tail -f 进程来监视文件,然后从该进程的输出中读取,只捕获 BYE 行并在前面打印一个自定义字符串。

(ns tail-example
  (:require [babashka.process :as p]
            [clojure.java.io :as io]))

(future
  (loop []
    (spit "my-file.txt" "HELLO\n" :append true)
    (spit "my-file.txt" "BYE\n" :append true)
    (Thread/sleep 1)
    (recur)))

(def tail (p/process
           (p/tokenize "tail -f my-file.txt")
           ;; send stderr to stderr of bb, leave out stream unmodified
           {:err :inherit}))

(let [rdr (io/reader (:out tail))]
  (binding [*in* rdr]
    (loop []
      (when-let [l (read-line)]
        (when (re-matches #"BYE" l)
          (println (str "[log] " l)))
        (recur)))))