Skip to content
Ilya Sher edited this page Oct 29, 2018 · 9 revisions

Short useful examples with explanations

List all top level functions in NGS

ngs -pi 'globals().filterv(Fun)'
  • globals() returns a Hash of all global variables
  • filterv() filters a Hash by predicate applied to values, resulting a new Hash
  • Fun - the function type (callable)
  • A type t, when used as predicate act as F(x) x is t, a predicate

Match all occurrences of a regular expression

Get all certificates from a certificate chain

certs = read(fileName) ~~ /-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----/ms

Parse openssl output (subject), convert to a Hash

subject = (`echo ${cert} | openssl x509 -subject -noout` - Sfx("\n") - Pfx("subject= /")).split("/").map(split(X, "=")).Hash()
  • `...` runs a command and captures the output, similar to bash
  • - Sfx("\n") removes newline at the end. See Sfx type.
  • - Pfx("subject= /") removes undesired prefix of the string. See Pfx type
  • .split("/") splits the line by /, resulting an array of strings. See split method
  • .map(split(X, "=")) splits each string in the array, resulting an array of the form [["k1", "v1"], ["k2, "v2"]]. See map method
  • .Hash() converts array of the form [["k1", "v1"], ["k2, "v2"]] to a Hash