cat

Q&A's, tips, howto's
Locked
Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

cat

Post by Sammo »

Having a need to concatenate files and not having a decent command-line tool, I wrote:

Code: Select all

;;  cat
;;  concatenate files
;;  c:> cat file1 file2 file3 ... >stdout
;;
;;  cat.make
;;  (load {c:\newlisp\link.lsp})
;;  (link {c:\newlisp\newlisp.exe} "cat.exe" "cat.lsp")

(map (fn (F) (write-line (read-file F))) (rest (main-args)))
(exit)
which seems to work pretty well. Is there a better sol'n?

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Hello Sammo,

Very Lispy !! great... now i was thinking exactly the same this week
except for a tool called "tail" You dont happen to have one perhpas :-) ?

Newlisp is great for baking tools this way ;-) love it...

Regards...Norman.
-- (define? (Cornflakes))

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

How about this?

1) read the entire file whose name is (nth 1 (main-args))
2) parse into lines
3) use 'slice' to get last (nth 2 (main-args)) lines (assumes an integer)
4) join and print the result

Code: Select all

;;  tail
;;
(print (join
        (slice
            (parse (read-file (nth 1 (main-args))) "\r\n")
            (- (integer (nth 2 (main-args)))))
        "\r\n"))
(exit)
Seems to work!

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

yes thats a real nice tail alright ;-)

If your into a new adventure?
Try tailing continously a file :-)
im not sure if newlisp can do it, but i doubt that its not able to do it ;-)

Regards, Norman...
-- (define? (Cornflakes))

Locked