Working with Files

Table of Contents

In many occasions you will need to read data from a file and process this information. RapidCMDB provides RSFileReader.groovy in the RS_HOME/RapidSuite/scripts directory to address this need.

You can have your script :

  • read the contents of the file and terminate (on-demand script)
  • listen to a file for newly appended lines (tail mode TRUE implementation with a scheduled script)
  • read existing lines and then listen for appends (tail mode FALSE implementation with a scheduled script)

The provided RSFileReader currently supports parsing each line for:

  • delimited values
  • fixed width values
  • name/value pairs

RSFileReader.groovy can be modified to alter behavior to handle more complex scenarios. Please review the code to get detailed information. Here are  the methods it supports:

  • getLines() // returns appended lines since the last poll. Returns existing lines during the first read if tailMode is set to false.
  • getDelimitedValues(line, delimiter)  //returns list of values based on the specified delimiter
  • getFixedWidthValues(line, widthList, trim)  //returns list of values based on specified field widths.
  • getFixedWidthValues(line, widthList)  //returns list of values based on specified field widths. No trimming.
  • getNameValueMap(line, fieldDelim, nameValueDelim)  //returns a map of names and values based on the specified fieldDelimeter separating name/value pairs from eachother and nameValueDelimiter separating names from values.

Typically, the scheduled script is configured to poll in short intervals, say, every second or 5 seconds. In this script, you can choose to get the lines appended to the file since the last polling of the file by using the getLines() method and implement custom parsing of these lines or you can use one of the line parsing methods provided with this class.

Here is a sample scheduled script:

// ************** FILE READER CONFIGURATION **************************

filePath = "c:\\temp\\input.txt";
tailMode = "true";  //  true/false

// **************  END OF FILE READER CONFIGURATION ******************

def fileReader = new RSFileReader(filePath, tailMode);
def lines = fileReader.getLines();

def widths = [3, 5, 5];

for (line in lines){
	println "DELIMITED VALUES: " + fileReader.getDelimitedValues(line, ",");
	println "FIXEDWIDTH VALUES: " + fileReader.getFixedWidthValues(line, widths);
	println "NAMEVALUE PAIRS: " + fileReader.getNameValueMap(line, "\\|", "=");
}

return lines.size();
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Anonymous says:

You are not logged in. Any changes you make will be marked as anonymous.