Friday, January 27, 2012

Repeated strings in bash

I just whipped up a little bar graph in bash using this trick: printf '=%.0s' $(seq 10)

A more complete example, using some file with lines containing a date string:

for d in {17..27} ; do
  N=$(grep 2012_01_$d /tmp/somefile | wc -l)
  echo -n "Jan $d: $N "
  printf "=%.0s" $(seq $N)
  echo
done
Jan 17: 28 ============================
Jan 18: 22 ======================
Jan 19: 43 ===========================================
Jan 20: 32 ================================
Jan 21: 0 =
Jan 22: 0 =
Jan 23: 46 ==============================================
Jan 24: 50 ==================================================
Jan 25: 50 ==================================================
Jan 26: 51 ===================================================
Jan 27: 41 =========================================

(Breaks for 0, as you can see)

If you're doing it for constant numbers, you can use {1..10} instead of $(seq 10), but {1..$N} doesn't seem to work.

No comments: