Created
August 1, 2012 14:41
-
-
Save crazy4groovy/3227413 to your computer and use it in GitHub Desktop.
Keep only X lines from tail of text file, trim off the remainder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String fName = "X:\\file_cache.temp" | |
| String bakupFileExt = ".bak.${(new Date().format('yyyyMMdd_hhmmss'))}" // if blank, file will be overwritten! | |
| int keepLines = 100000 | |
| File oldF | |
| List lines | |
| try { | |
| oldF = new File(fName) | |
| lines = oldF.readLines() | |
| //println lines.size() | |
| } | |
| catch (Exception e) { println "ERROR: Cannot open file $fName\n$e"; return } | |
| int startI = lines.size() - keepLines | |
| lines = lines[startI..-1] | |
| //println lines.size() | |
| if (bakupFileExt) { | |
| oldF.renameTo(fName+bakupFileExt) | |
| } | |
| File newF = new File(fName) | |
| newF << lines.join('\r') | |
| return "DONE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment