Text-Rewrite: A Text-Transforming Ant Task.
By Charles M. Chen, charlesmchen@gmail.com.
Description
This Ant Task performs a variety of simple operations on text files.
Although not yet version 1.0, Text-Rewrite is working and can be easily intergrated into your build process.
It is Open Source; free as in freedom and free as in beer.
Downloads
Version 0.75 (September 5th, 2006)
Binary: text-rewrite.jar
Source: text-rewrite-src.zip
Source distribution includes binary, source and this document. The source also depends on private libraries that I haven't yet published, but will soon.
Usage
Step 1: Download the binary.
Step 2: Adds a task definition to your project, like so:
 
<taskdef name="TextRewrite" classname="org.cmc.textrewrite.TextRewriteTask" classpath="${libs}/text-rewrite.jar" />
You can either specify the classpath in the task definition (as above), if you haven't added it to ant's classpath in another way (for example, at invokation).
Step 3: Invoke TextRewrite, for example:
 
<TextRewrite>

	<fileset dir="${src-dist}">
	<include name="**/*.html" />
	</fileset>

	<lineCount />
	<searchAndReplace find="findthis" replace="replacewiththis" />
	<changeCase mode="title" />
	<lineCount />
	<stripEmptyLines />
	<lineCount />
	<tail limit="20" />
	<lineCount />
	
</TextRewrite  >
Note that you can apply any number of text transformations with a single invocation of TextRewrite. Here we a) search & replace, b) Change to Title Case, c) Strip Empty Lines and d) tail, printing out line counts as we go.
Text Transformations
Append
Append a string to the right/end of each line.
Args:
Value (String)
Sample Usage: <append value="&#9;// comment " />
line 1 line 2
yields:
line 1 // comment // comment line 2 // comment
ChangeCase
Change case of each line.
Args:
Mode (upper/lower/title)
Sample Usage: <changeCase mode="title" />
line 1 line 2
yields:
Line 1 Line 2
ClipLeft
Remove characters from the left/beginning of each line.
Args:
Count (Optional number, default: 1)
Sample Usage: <clipLeft count="2" />
line 1 line 2
yields:
ne 1 ne 2
ClipRight
Remove characters from the right/end of each line.
Args:
Count (Optional number, default: 1)
Sample Usage: <clipRight />
line 1 line 2
yields:
line line
EscapeXML
Escapes XML/HTML reserved characters with entities.
Args: None.
Sample Usage: <escapeXML />
<escapeXML />
yields:
&lt;escapeXML /&gt;
Head
Remove all but the first n lines of each file.
Args:
Limit (Optional number, default: 100)
Sample Usage: <head limit="1" />
line 1 line 2
yields:
line 1
Left
Remove all but the first n characters of each line.
Args:
Count (Optional number, default: 1)
Sample Usage: <left count="3" />
line 1 line 2
yields:
lin lin
LineCount
Prints a line count, without changing the file.
Args: None.
Sample Usage: <stripEmptyLines />
line 1 line 2
yields:
line 1 line 2
Prepend
Prepend a string to the left/beginning of each line.
Args:
Value (String)
Sample Usage: <prepend value=" > " />
line 1 line 2
yields:
> line 1 > > line 2
PrependLineCount
Prepend a running line count to each line.
Args: None.
Sample Usage: <prependLineCount />
line 1 line 2
yields:
1: line 1 2: 3: line 2
ReverseLine
Reverses each line, character-by-character.
Args: None.
Sample Usage: <reverseLine />
line 1 line 2
yields:
1 enil 2 enil
Right
Remove all but the last n characters of each line.
Args:
Count (Optional number, default: 1)
Sample Usage: <right count="3" />
line 1 line 2
yields:
e 1 e 2
SearchAndReplace
Search & Replace
Args:
find (String)
replace (String)
Sample Usage: <searchAndReplace find="line" replace="li^ne" />
line 1 line 2
yields:
li^ne 1 li^ne 2
Sort
Sorts the file by line.
Args:
ignoreCase (Optional boolean, default: false)
Sample Usage: <sort ignoreCase="true" />
d c a b
yields:
a b c d
StripEmptyLines
Removes blank lines from the file.
Args: None.
Sample Usage: <stripEmptyLines />
line 1 line 2
yields:
line 1 line 2
Tail
Remove all but the last n lines of each file.
Args:
Limit (Optional number, default: 100)
Sample Usage: <tail limit="1" />
line 1 line 2
yields:
line 2
Trim
Removes whitespace from the beginning and end of each line.
Args: None.
Sample Usage: <stripEmptyLines />
line 1 line 2 line 3
yields:
line 1 line 2 line 3
Project status
Version 0.75 released September 5th, 2006.

To do list:
License
This text is available under the ASF (Apache) License.
This is a non-viral Open Source license.
home