This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Wobbo's profile picture

Cc Grep

Started by Wobbo, 22 November 2013 - 03:09 PM
Wobbo #1
Posted 22 November 2013 - 04:09 PM
This is an implementation of (almost) POSIX grep for computer craft. It uses Lua regular expressions for matching, and this is the only difference between my version and a POSIX complaint one, as far as I can tell.
It is my first real program in Lua.

Grep looks for occurrences of patterns in files. If it finds one, it prints the line that it occurred on. As said before, the patterns are lua regular expressions. The program is called like this:

grep pattern files
Where pattern is one pattern and files is a list of one or more files.
Multiple patterns can be specified like this:

grep -e pattern -e pattern files
Patterns can also be read from a file:

grep -f patternfile files
grep -e pattern -f patternfile files
grep -f patternfile -f patternfile files

-F turns on plain matching, aka no character will be seen as magic. -i makes grep ignore case in patterns. -x makes patterns only match if they match the whole line. -v inverts grep's behaviour, it will only print lines that do not match the specified pattern.

-n prints a line number in front of the matching line, while -c only prints a count of matching lines.

-l prints the name of the searched file only once when a pattern has been found.
-s stops the printing of file not found errors.

-E and -q are quietly accepted, but don't do anything (actually, -q works, but os.run doesn't return the correct values)

Grep can be found here or installed using pastebin:

pastebin get E1PNzrA2 grep

If you find any bugs/inconsistencies please reply in this thread.
leftshiftadri #2
Posted 20 December 2013 - 12:59 PM
Looks pretty cool. Unfortunalty, without the ability to pipe it won't work for most of the things I use grep for.
Wobbo #3
Posted 20 December 2013 - 04:02 PM
No, without piping it isn't that much use. But you could use it to look up where you use variables.

But there are some shells/OSes that do allow for piping, for example Leadlined OS and Project NewLife both support piping. There might be more.