Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate two files and redirect output with Clojure using terminal

I'm trying to reach out to the terminal in Clojure to concatenate two binary files together.

So I'm trying to do something like: cat file1 file2 > target

I've started looking at conch but I can't seem to get cat to treat my inputs as file paths rather than strings, e.g.

(def files '["/tmp/file1" "/tmp/file2"])

(defn add-to-target [files target]
  (cat {:in files :out (java.io.File. target)}))

(add-to-target files "/tmp/target")

The result written to the /tmp/target file is:

/tmp/file1
/tmp/file2

I'm happy to try other (perhaps more Clojure idiomatic) ways to do this.

Thanks in advance.

like image 279
Ciaran Archer Avatar asked Feb 14 '23 03:02

Ciaran Archer


1 Answers

Here you go:

(ns user
  (:require [clojure.java.io :as io]))

(defn catto [f1 f2 out]
  (with-open [o (io/output-stream out)]
    (io/copy (io/file f1) o)
    (io/copy (io/file f2) o)))

;; in REPL
;; > (catto "station.mov" "super.pdf" "zzz.bin")

Take a look at clojure.java.io docs.

like image 104
edbond Avatar answered Apr 30 '23 00:04

edbond