Java library for processing text / string data using a simulator for unix / linux utilities

I am a Java programmer. I use bash scripts to process text.

Utilities such as grep, sed, awk, tr, wc, find, as well as the connection between commands give such a powerful combination.

However, bash programming does not have the portability, testability, and more elegant programming constructs that exist in Java. It also makes integration with other Java products difficult.

I was wondering if anyone knows of any Java word processing libraries that can offer what I'm looking for.

It would be great to write:

Text.createFromFile("blah.txt).grep("-v","ERROR.*").sed("s/ERROR/blah/g").awk("print $1").writeTo("output.txt") 

It could be pie material in the sky. But I thought I would leave the question anyway.

+6
source share
2 answers

Unix4j implements some basic unix commands, mainly focusing on word processing (with support for connection 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 participates in the unix4j project
+2
source

Believe it or not, I used the built-in Ant for many of these tasks.


Update

Ant has a Java api that allows it to be called from Java projects. This is the built-in mode. This is a link to and Api 1.6.1 . Distribution should also include documents.

To use it, you must create a new task object, set the appropriate parameters and execute it in the same way as in build.xml, but through Java Api. How can you accomplish your task.

Sort of

 ReplaceRegExp regexp = new ReplaceRegExp(); regexp.setMatch("bla"); regexp.setFile(new File("inputFile")); regexp.execute(); 

You may need to configure other things.

Not sure if it solves your problem, but Ant has a lot of code to do something. Just search the docs.

+2
source

Source: https://habr.com/ru/post/892627/


All Articles