align 534 B

1234567891011121314151617181920212223242526272829
  1. #!/bin/sh
  2. # kind of like emacs' regex-align command
  3. # How it work: align '::' file.txt
  4. cat $2 | awk -F$1 '{
  5. if(max < length($1)) {
  6. max = length($1)
  7. }
  8. list[NR] = $0
  9. }
  10. END {
  11. for(i in list) {
  12. if(list[i] ~ FS) {
  13. split(list[i], a, FS);
  14. diff = max - length(a[1])
  15. if(diff == 0) {
  16. print a[1] FS a[2]
  17. } else if(diff > 0) {
  18. spaces = ""
  19. for(; diff > 0 ; diff--)
  20. spaces = spaces " "
  21. print a[1] spaces FS a[2]
  22. }
  23. } else {
  24. print list[i]
  25. }
  26. }
  27. }'