Use SED or Groovy to print paths, split with line feeds

Yea, this is easy, IF you use SED much. Now I updated this post with a Groovy approach, since this is easier to install where cygwin may be prohibited.

Using SED
With cygwin installed, SED, the stream editor is available. In a command shell, execute:

set path | sed s_;_;\n_g

Update: fixed the sed above, was missing the backslash before the n.

Explanation

  1. set path will print the Windows path. File path entries are separated by “;”.
  2. sed will invoke the cygwin installed linux SED command. Cygwin\bin is part of the executable path.
  3. “s” indicates the substitute command
  4. “_”, the underscore is used as the delimiter to each part of the substitution. One can use other characters for the delimiter.
  5. “;” is the regular expression to use for a match.
  6. “;\n” is the string to substitute with. \n is the crlf.
  7. “g” is the substitute flag, global replacement

No doubt there are more direct ways of doing this. Using PowerShell would be the most appropriate on Windows if it is available.

Using Groovy
Another approach is using the Groovy language, which has a command line mode using the “-e” switch:

set path | groovy -e "System.in.text.split(';').each{ println it}"

Explanation
The output of set path is piped to an inline Groovy script. The script uses the “text” value of the System.in standard input stream. ‘text’ is some magic Groovy dust. The Groovy development kit (GDK) extends Java io streams with a method, textO(), that gets the string value of the stream, like read a file. This is available as a java bean getter method, which again via magic is available as a field, “.text”. The string is then split, then a closure “each” is used to print each line, passed to the closure with the default variable “it”.

Example
If your path is: C:\fee;d:\fi;c:\foo;fum

The result would be:
C:\fee;
d:\fi;
c:\foo;
fum

You can, of course, pipe these together to allow finer control. However, the high level language makes for very long winded code, for example, find all properties in Java system properties that have the word ‘Java’ or ‘java’:

groovy -e "System.properties.each{println it}" | groovy -e "System.in.eachLine{ if(it.find('[jJ]ava')) println it}"

Better would be, just putting the filtering inside the closure:

groovy -e "System.properties.each{ if(it.key.find('[jJ]ava')) println it}"

Or as a script:

System.properties.each{ 
     if(it.key.find('[jJ]ava')){ 
        println it
     }
}

Consult the Groovy documentation for further Groovy syntax and idioms to make the above even better.

References

  1. GNU sed
  2. Sed – An Introduction and Tutorial by Bruce Barnett
  3. SED, stream editor
  4. PLEAC-Groovy

Similar Posts:

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

Leave a Reply

Your email address will not be published. Required fields are marked *