Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Last active September 13, 2023 14:47
Show Gist options
  • Save aimtiaz11/2cbade28acf1ca966acd265187f19175 to your computer and use it in GitHub Desktop.
Save aimtiaz11/2cbade28acf1ca966acd265187f19175 to your computer and use it in GitHub Desktop.
Helpful Groovy Scripts
/**
* Usage: groovy compare-list.groovy first.txt second.txt
**/
def sourceList = []
new File( args[0] ).eachLine { line ->
sourceList << line.trim()
}
def targetList = []
new File( args[1] ).eachLine { line ->
targetList << line.trim()
}
List diff1 = (sourceList-targetList)
List diff2 = (targetList-sourceList)
List diff3 = sourceList.intersect(targetList)
println """
\n
println "################ COMPARE LIST #########################
\n
"""
println "- Total in sourceList: ${sourceList.size}"
println "- Total in targetList: ${targetList.size}"
println "\n\n**** Items in '${args[0]}' not in '${args[1]}' list: ${diff1.size} ****\n"
diff1.each { println "\t${it}" }
println "\n\n**** Items in '${args[1]}' not in '${args[0]}' list: ${diff2.size} ****\n"
diff2.each { println "\t${it}" }
println "\n\n***** Common items: ${diff3.size} *****\n"
diff3.each { println "\t${it}" }
println """
\n
######################################################
\n
"""
/**
* Usage: groovy distinct-list.groovy input.txt
*/
def sourceList = []
new File ( args[0] ).eachLine { line ->
sourceList << line.trim()
}
Set<String> set = new HashSet<String>();
sourceList.each {
set.add(it);
}
println """
\n
################ DISTINCT LIST #########################
\n
"""
set.each {
println it
}
println """
\n
Original Entries: ${sourceList.size()}
Distinct Entries: ${set.size()}
\n
######################################################
\n
"""
/**
* Usage: groovy print-csv.groovy input.txt
*/
def sourceList = []
new File ( args[0] ).eachLine { line ->
sourceList << line.trim()
}
// This part ignores any items after colon. If you have host1:port1, host2:port2 you will get host1,host2 in CSV print.
// comment out the use of this function if it doesnt apply to you
def processEachEntry = { e ->
def length = e.split(":").length
if(length == 1)
return e;
else if (length == 2)
return e.split(":")[0]
else
throw new RuntimeException("Invalid entry - " + length + ' ' + e) // wont accept more than 1 colons
}
def updatedList = sourceList.collect { processEachEntry(it) }
print """
\n
############## PRINT CSV ############################
\n
${updatedList.join(",")}
\n
Total entries: ${updatedList.size()}\n
\n
######################################################
\n
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment