Right alignment - bash

I have one problem. My text should be aligned to the correct width. I managed to reduce the output volume to the right size, but I have a problem with placing everything on the right side.

Here is what I got:

#!/usr/local/bin/bash

length=$1
file=$2
echo $1

echo -e "length = $length \t  file = $file "
f=`fold -w$length $file > output`
while read line
do
        echo "line is $line"
done < "output"

thank

+3
source share
2 answers

Try:

printf "%40.40s\n" "$line"

This will result in a right-justification with a width of 40. If you don't want to truncate, drop it .40(thanks Dennis!):

printf "%40s\n" "$line"

For instance:

printf "%5.5s\n" abc
printf "%5.5s\n" abcdefghij
printf "%5s\n" abc
printf "%5s\n" abcdefghij

will print:

  abc
abcde
  abc
abcdefghij
+16
source

Your last step may be

sed -e :a -e 's/^.\{1,$length\}$/ &/;ta'
+1
source

Source: https://habr.com/ru/post/1776093/


All Articles