arrlen.fig 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #### arrlen prefixes length to each line of text
  2. #### license: creative commons cc0 1.0 (public domain)
  3. #### http://creativecommons.org/publicdomain/zero/1.0/
  4. #### ive been thinking about the tasks i perform most often in
  5. #### bash, and of making a few utilities to create shortcuts.
  6. #### bash is a great language i use every day, though im not an
  7. #### expert. so these utilities will not replace bash, but for
  8. #### me they augment it.
  9. #### they can also be used to teach coding.
  10. #### the first of these utilities is called arrlen, which takes
  11. #### stdin and returns the length of each line. yes, you can do
  12. #### this with a bash one-liner; ive chosen fig.
  13. # p=$(echo hello | arrlen | awk '{print $1}') ; echo $p
  14. # 5
  15. # $ for p in $(echo "hello there how are you?") ; do echo $p ; done | arrlen
  16. # 5 hello
  17. # 5 there
  18. # 3 how
  19. # 3 are
  20. # 4 you?
  21. # $
  22. function re(z p)
  23. f=z ; split(f p) ; join(f "") ; return(f)
  24. fig
  25. forin (p stdin)
  26. cr=13 ; chr
  27. lf=10 ; chr
  28. z=p ; re(z cr) ; re(z lf)
  29. p=z ; len ; prints ; " " ; prints
  30. p=z ; print
  31. next