Page 1 of 1

newlisp with filter function (unix only)

Posted: Sat May 19, 2012 8:01 pm
by 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 :-)

Re: newlisp with filter function (unix only)

Posted: Sun May 20, 2012 7:15 am
by Lutz
The following examplewould work like a filter:

Code: Select all

#!/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:

Code: Select all

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 for a sub chapter "File filters"

Re: newlisp with filter function (unix only)

Posted: Sun May 20, 2012 7:50 am
by newdep
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:

Code: Select all

# 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 :-)

Re: newlisp with filter function (unix only)

Posted: Sun May 20, 2012 8:21 am
by Lutz
Same thing, use 'read-line' for STDIN:

Code: Select all

~> 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'.

Re: newlisp with filter function (unix only)

Posted: Sun May 20, 2012 8:35 am
by newdep
Of all options i did not test read-line... how stupid of me ;-) Thanks..