newlisp with filter function (unix only)

Started by newdep, May 19, 2012, 01:01:27 PM

Previous topic - Next topic

newdep

Hi Lutz,



is it possible to extend newlisp so that its capable of beeing a 'filter'.

So it accepts input during startup.



example:



echo "(println "hello newlisp")" | newlisp -e '(eval $main-args)'



I know $main-args wont work here but its just an example.



I would like to use newlisp in the command stream of linux this way.



Hope its something you to concider?



Greetings :-)
-- (define? (Cornflakes))

Lutz

#1
The following examplewould work like a filter:


#!/usr/bin/newlisp
# demo filter script as pipe
# example:
#          ./filter < my-text

(while (read-line) (println (upper-case (current-line))))
(exit)


you could also use it like this:


cat mytext.txt | filter | grep 'FOO BAR'


instead of 'upper-case' you could do anything else on the incoming lines.



See also here: http://www.newlisp.org/CodePatterns.html#toc-2">http://www.newlisp.org/CodePatterns.html#toc-2 for a sub chapter "File filters"

newdep

#2
Hi Lutz,



...yes yes i know :-), but my suggestion is more related to newlisp as being a command-line filter

and not such as being able to filter, its about 'not' creating a file/script when using newlisp but

about using newlisp on the command-line.



newlisp has functions that can be used on the regullar command-line in the OS, outside newlisp.





Just simple example on how it could be used:
# assuming $IN eats from STDIN on the commandline
echo 234.234 | newlisp -e '(add $IN 24.4234)'
258.6574

#or
cat myfile | newlisp -e '(explode $IN)'

#or
firstscript.lsp | newlisp -e '(net-send-udp "host.com" 1233 $IN)'

# or






Greetings :-)
-- (define? (Cornflakes))

Lutz

#3
Same thing, use 'read-line' for STDIN:


~> echo hello | newlisp -e '(upper-case (read-line))'
"HELLO"
~> echo 123.456 | newlisp -e '(mul 2 (float (read-line)))'
246.912


but for multiple lines you would use '(while (read-line) (do-thing-with (current-line)))'.



Probably you are looking for automatic iterating over the input without 'while'.

newdep

#4
Of all options i did not test read-line... how stupid of me ;-) Thanks..
-- (define? (Cornflakes))