【问题标题】:How to filter output of tail with babashka如何用 babashka 过滤尾部的输出
【发布时间】:2021-09-19 07:01:51
【问题描述】:

我想过滤和修改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”命令。 有什么想法吗?

谢谢

【问题讨论】:

  • 你能描述一下tail -f遇到的问题吗?
  • 以上脚本适用于“tail /tmp/data.txt | script.clj”,但“tail -f /tmp/data.txt | script.clj”失败
  • 好的。但是有很多方法会失败。你能描述一下它失败的方式吗?
  • 它只是挂在那里没有任何输出。

标签: clojure babashka


【解决方案1】:

此示例开始向文件写入两种消息: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)))))

【讨论】:

    猜你喜欢
    • 2016-07-09
    • 1970-01-01
    • 2020-03-07
    • 2013-01-19
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多