Bourne Shell Left Right Justify

user174084 picture user174084 · Feb 4, 2010 · Viewed 10.8k times · Source

I am trying to do some formatting on output data in a script and not positive how to do Left Right justify as well as width. Can anyone point me in the right direction?

Answer

ghostdog74 picture ghostdog74 · Feb 4, 2010

you can use printf. examples

$ printf "%15s" "col1"
$ printf "%-15s%-15s" "col1" "col2"

tools like awk also has formatting capabilities

$ echo "col1 col2" | awk '{printf "%15s%15s\n", $1,$2}'
           col1           col2