'Write a List of Strings into CSV file

list[0] = "ilocpde406,Pass,Pass,w38,Used,oc-cd-22-02"
list[1] = "ilocpde406,Pass,Pass,w39,Available,NA"
list[2] = "ilocpde406,Pass,Pass,w40,Available,NA"

Output:

Cluster      NTNET INTEG  NodeName  Status     Namespace
ilocpde406   Pass  Pass   w38       Used       oc-cd-22-02
ilocpde406   Pass  Pass   w39       Available  NA
etc..

I thought about splitting using the comma, and appending each line with all the elements, but I was wondering if there is a better approach for this in Jenkins.



Solution 1:[1]

Using split / replace is not a bad idea.

Down below is one of thousand ways to do it:

def list = []
list << 'Cluster,NTNET,INTEG,NodeName,Status,Namespace'
list << "ilocpde406,Pass,Pass,w38,Used,oc-cd-22-02" 
list << "ilocpde406,Pass,Pass,w39,Available,NA" 
list << "ilocpde406,Pass,Pass,w40,Available,NA"

StringWriter out = new StringWriter() // or File
list.eachWithIndex{ item, ix ->
  if( ix ) out << '\n'
  item.split( ',' ).each{ out << it.padRight( 12, ' ' ) }
}
out

prints

Cluster     NTNET       INTEG       NodeName    Status      Namespace   
ilocpde406  Pass        Pass        w38         Used        oc-cd-22-02 
ilocpde406  Pass        Pass        w39         Available   NA          
ilocpde406  Pass        Pass        w40         Available   NA     

If you want do add fancyness, you can scan the list for max column sizes and padRight each item with respective size.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 injecteer