Advertisement
devinteske

environ8r

Jul 10th, 2018
3,615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 0.52 KB | None | 0 0
  1. #!/usr/bin/awk -f
  2. BEGIN { w = "[a-zA-Z_][a-zA-Z0-9_]*"; var = sprintf("\\$(%s|{%s})", w, w) }
  3. {
  4.     str = ""
  5.     tail = $0
  6.     while (match(tail, var)) {
  7.         head = substr(tail, 1, RSTART - 1) # text before match
  8.         repl = substr(tail, RSTART, RLENGTH) # match to replace
  9.         tail = substr(tail, RSTART + RLENGTH) # text after match
  10.         if ((match(head, /\\+/) ? RLENGTH + 1 : 1) % 2 == 1) {
  11.             sub(/^\$/, "", repl)
  12.             gsub(/(^{|}$)/, "", repl)
  13.             repl = ENVIRON[repl]
  14.         }
  15.         str = str head repl
  16.     }
  17.     str = str tail
  18.     print str
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement