HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Constructing a tail cmd

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
tailconstructingcmd

Problem

I wrote this without much thought:

(defn- tail-cmd
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (clojure.string/join
   " | "
   (flatten [(format "tail -n %s -f %s" (or n 200) file)
             (map #(format "grep --line-buffered -v \"%s\"" %)
                  ignore-patterns)])))


On further thought, I'm not sure if it can simplified further. Can it be?

Solution

You may or may not consider this simpler:

(defn tail-cmd-2
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (str "cat "
       file
       " | "
       (apply str (map #(format "grep -v \"%s\" | " %) ignore-patterns))
       "tail -f "
       (if n (str "-n " n))))


Or if you're willing to use the non-standard clojure.core.strint library, you can do the following.

(defn tail-cmd-5
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (let [greps   (apply str (for [p ignore-patterns] (format "grep -v \"%s\" | " p)))
        minus-n (if n (str "-n " n))]
    (<< "cat ~{file} | ~{greps} tail -f ~{minus-n}")))


Here's the library source:
https://github.com/clojure/core.incubator

Code Snippets

(defn tail-cmd-2
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (str "cat "
       file
       " | "
       (apply str (map #(format "grep -v \"%s\" | " %) ignore-patterns))
       "tail -f "
       (if n (str "-n " n))))
(defn tail-cmd-5
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (let [greps   (apply str (for [p ignore-patterns] (format "grep -v \"%s\" | " p)))
        minus-n (if n (str "-n " n))]
    (<< "cat ~{file} | ~{greps} tail -f ~{minus-n}")))

Context

StackExchange Code Review Q#5777, answer score: 3

Revisions (0)

No revisions yet.