Java library for text/string processing simular to unix/linux utilities - java

I'm a java programmer. I use bash scripts a lot for text processing.
Utilities like grep,sed,awk,tr,wc,find, along with piping between commands gives such a powerful combination.
However bash programming lacks portability, testability and the more elegant programming constructs that exist in Java. It also makes it harder to integrate into our other Java products.
I was wondering if anyone knows of any Java text processing libraries out there which might offer what I'm looking for.
It would be so cool to be able to write:
Text.createFromFile("blah.txt).grep("-v","ERROR.*").sed("s/ERROR/blah/g").awk("print $1").writeTo("output.txt")
This might be pie-in-in-the-sky stuff. But thought I'd put the question out there anyway.

Unix4j implements some basic unix commands, mainly focussing on text-processing (with support for piping between commands): http://www.unix4j.org
Example (Ben's example, but without awk as this is not currently supported):
Unix4j.fromStrings("1:here is no error", "2:ERRORS everywhere", "3:another ERROR", "4:nothing").toFile("blah.txt");
Unix4j.fromFile("blah.txt").grep(Grep.Options.v, "ERROR.*").sed("s/ERROR/blah/g").toFile("output.txt");
Unix4j.fromFile("output.txt").toStdOut();
>>>
1:here is no error
4:nothing
Note:
the author of the question is involved in the unix4j project

Believe it or not, but I used embedded Ant for many of those tasks.
Update
Ant has Java api's that allow it to be called from Java projects. This is embedded mode. This is a reference to And Api 1.6.1. Distribution should include docs as well.
To use it, you would create new task object, set appropriate parameters and execute it just as you would in build.xml but via Java Api. Than you can run your task.
Something like
ReplaceRegExp regexp = new ReplaceRegExp();
regexp.setMatch("bla");
regexp.setFile(new File("inputFile"));
regexp.execute();
You may need to set up some other stuff as well.
Not sure if it solves your problem, but Ant has a lot of code to do things. Just search through docs.

Related

How do I use Jython to script Java programs

I am working on a project, which will introduce programmable computers to Minecraft, similar to ComputerCraft, except using Python as opposed to lua.
I am aware of Jython, so thought it would be suitable to check if I could use that for my project, however couldn't find enough information (on their website and with a few searches) to be certain.
I am aware of the topic discussing using Java from within Jython, however this is not how I want my project to work. Those that have used Computercraft, know that you have only the libraries that Computercraft provides you, whereas the topic linked above has full access to.. well everything. In my use case, everything isn't possible. I also don't want from pycomputers.api import Colors, I want the 'colors' api to be used like colors.red.
Hopefully the above is possible, within Jython, if not I would love to know another Python interpreter (that can be used from Java), to make my project with.
According to the docs if you want to embed the Jython into a java application then you have to invoke it with the PythonInterpreter class. It's pretty strait forward from there, just note that the overloads that provide a filename argument are for the name of the main executable file (this information is normally available through the sys module in regular old CPython).
Now in order to expose bindings in java to python (say to control the minecraft world), we need to add the jar files to sys.path as is described here, and if you want to control the sys.path value from within java then use
PythonInterpreter pi = new PythonInterpreter();
pi.getSystemState().path.append(new PyString("path/to/java/modules.jar"));
Note that the packages inside the jar files cannot be organized in the reverse url style. Instead, follow the instructions on package naming. Particularly make note of Naming Python Modules and Packages and if you follow the guidelines in Proper Python Naming then you will get the results you desire.
Finally we execute the code with
String source = ...;
pi.execute(pi.compile(source));

Java command line "shell" with auto-complete

I need to create a Java command line to that will be invoked remotely on a server. It will need to:
Read "lines" of text from the user.
Recognise if the user presses the "tab" key to facilitate auto-complete.
Recognise if the user presses the "up/down" keys for history.
Before I go off and roll my own, Is anyone aware of a Java library that might facilitate all or part of this?
i.e. From the command line in ssh it might look like this:
bob> java -jar MyTool.jar
MyTool Started.
Please enter command:
> server1 startup
server starting...
server started
> server2 load accounts
Done
> server3 shutdown
Complete
>quit
Check out JReadline and jline2.
Update: picocli-shell-jline2 and picocli-shell-jline3 combine the strength of JLine 2 and 3 with picocli.
Picocli allows you to write commands (and subcommands) with options and positional parameters using very little code, and the library will generate JLine 2 and 3 command completers for all commands.
It uses ANSI colors and styles in the usage help message, and has many other unique features like negatable options, repeatable nested argument groups, variable interpolation and more.
Disclaimer: I am the author and therefore biased.
it seems like you're trying to ask 3 different questions at once and don't know what you really want an answer to.
accepting user input and providing auto-complete is trivial and i highly doubt you will find a generalized library for such a task
parsing complex bash-like statements sounds like something cool to have and a library may exist to do that, but i don't think it would give you much headroom to create your own set of bash-like instructions. (especially considering you say it needs to be more sophisticated than anything you could do as a bash script - which is a tall order)
parsing simple user input as if it was a command-line input or command is also rather trivial, and if this is what you are looking for, you should look at this possible duplicate: How to parse command line arguments in Java?
i recommend restructuring your question to be more specific in exactly what you are looking for and to avoid putting emphasis on the trivial task of "auto-complete" and simply accepting the users input in a text box.
Have you taken a look at the BeanShell? It doesn't act like a shell proper (like bash or csh) but it'll let you type java commands like an interpreter and you can use tab to autocomplete.
I've only used the 1.X versions of bean shell but they always open a window for you so it's not something you can run inside an existing shell.
I assume you mean something similar to the python interpreter. The reason there is not equivalent in Java is because Java needs to be compiled to bytecode before it can be executed.
If you are looking for something with good auto-complete capabilities. I would recommend eclipse or netbeans. They also compile your application automatically, allowing you to quickly run your code once you are done writing it.
Hope that helps.
Bit late to the party on this one, but I'd add Crash and Cliche to the mix.
Apache Karaf has a command shell which can be used as a library to build a command shell for other applications. You create classes to represent commands, and use annotations to specify the options to the commands. The Karaf library provides tab completion, history and line editing, and the ability to run interactively or read in files or command line arguments to execute as a script.
I found out about it here and have used it in my own project; it works quite well. I can't compare it to any of the others as I haven't used them.

In Java how can i do here docs? Like BASH or PHP we can

How can i do with Java. Here doc like string? Exaple:
String java = << \EOF
#This file is written via Java
#You are watching JavaHereDoc
; comments ;
value=abc
etc etc
EOF;
System.out.println(java); shows exactly like above. How can i do this?
Java (as of 7) doesn't support HERE docs (also known as multiline strings) unfortunately.
If you're trying to accomplish templating, there are a few options:
StringTemplate
Apache Velocity
These aren't exactly similar to HERE docs in Perl or PHP since the string that describes the template isn't directly in your code; it's usually in a separate file.
There was a proposal put forward by Stephen Colebourne as well as a proposal via Project Coin, neither of which made it into Java 7, which was a little disappointing. Languages like Groovy and Scala, which also run on the JVM do support multiline strings.
In the meantime if you find it useful here's a basic swing application I use to do this for SQL that's embedded into the classes. I find myself using it over and over again so why not share it?
https://github.com/danielbchapman/Swing-String-Escaping-Utility
(a small note, its throw away code since I put it together in a week where I needed to rapidly format queries, I might clean it up at some point)

Is there any program like LINQPad for Java?

I've found LINQPad to be extremely useful when answering StackOverflow questions for C# or VB.NET. It allows me to write up some quick code, run it, and (if I want) see a nicely-formatted dump of the results. That way I can be sure that the code I post actually runs. Thus far I haven't seen anything that I can use to achieve the same result with Java. Is there anything like that out there?
I am not looking for something to query data sources; I just want a light-weight IDE. These are the features I'm particularly interested in:
The ability to write and run short snippets of code without establishing a whole project or file structure.
Reporting of compiler and runtime errors in the code when it is run.
The ability to add references to a particular editor instance.
Syntax highlighting and Autocomplete/Intellisense would be a plus.
JPad - A java scratchpad for running snippets
Since I also couldn't find one I've decided to write one. Currently it can:
Run java snippets (no class / imports / public blah... needed).
Contains drivers for MS/MySQL/Postgres.
Output results as HTML tables
It's very rough but I will add to it over time. Feedback is definitely welcome.
This may help : http://www.browxy.com:9000/codeRunner
EDIT: Url seems to have changed to http://www.browxy.com
You can use the Groovy web console ; it's possible to speak java in groovy land.
Java Snippet Runner:
Does something similar to Linqpad (jar file, not just for macs)
http://mac.softpedia.com/get/Development/Java/Java-Snippet-Runner.shtml
Code Runner (Commercial):
for Mac's only, it'll run code snippets in Java, and lots of other languages too (e.g. Objective C)
http://krillapps.com/coderunner/
http://ideone.com is an online service that has the features you want.
I've been using JEdit for a long time, which is a very powerful cross-platform editor, NOT an IDE. It does have plugins to execute Java code right in the editor, and even uses BSH for macros.
I was looking for a "Java LinqPad" also, and i came across :
this
I've been using IntelliJ IDEA and it works really well as a Groovy scratchpad. The Community Edition is free too.
You need to create a new project, but then can add Groovy scripts to it and run them on the fly. Not had any luck with the actual Scratch File functionality though.
Being a Jetbrains editor it's pretty slick too. (Unlike some of the other options)
Nothing beats LinqPad though.

How should I call a Perl Script in Java?

I read Runtime.getRuntime().exec("perl script.pl") is an option, but is this the best way to do it?
I'll need an answer from that script, so I'll have to read the script's return in some cases, although I might read it from a text file on other cases.
Anyway, is exec() a good way of calling a Perl Script from Java? I should note, I'm working on a Java Web Application, so security is an issue here.
You can use Runtime.getRuntime().exec() or use the Process API.
The Process API allows you to get the output of the script, so you can have both communicate.
exitValue() and getInputStream() seems to be what you need.
This outlines how to do it fairly elegantly, though it may be more effort than it's worth:
http://search.cpan.org/~nwclark/perl-5.8.9/jpl/docs/Tutorial.pod
Overview:
Well-supported by JPL, but it is a complicated process:
The JPL preprocessor parses the .jpl file and generates C code wrappers for Perl methods. It also generates Java and Perl source files.
The C compiler compiles the wrapper and links it to the libPerlInterpreter.so shared library, producing a shared library for the wrapper.
The Java compiler compiles the Java source file, which uses native methods to load the wrapper.
The wrapper connects the Java code to the Perl code in the Perl source file.
Fortunately, a generic Makefile.PL simplifies the process. This is a Perl script that generates a Makefile for you.
exec() is likely the best option and you can catch it's return value with exitValue(). You might also be interested in Inline::Java.
-John
keep in mind, whatever file the Perl script create, it is created in the Java working folder. just refer to that file as './myPerlCreatedFile.ext'

Categories