6

I have one problem. My text should be aligned by right in specified width. I have managed to cut output to the desired size, but i have problem with putting everything on 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"

thanks

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
cubrilo
  • 63
  • 1
  • 1
  • 3

3 Answers3

21

Try:

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

This will make it right-aligned with width 40. If you want no truncation, drop .40 (thanks Dennis!):

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

For example:

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

will print:

  abc
abcde
  abc
abcdefghij
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • this give me some strange output, don't know why – cubrilo Nov 21 '10 at 17:22
  • :) Do you think I can guess what the output was? Please post the problem in another comment. If it's too long, edit your question and post it there. – icyrock.com Nov 21 '10 at 17:25
  • @cubrilo: You might want a `\n` newline so the output matches the intent implied in the original: `printf "%40.40s\n" "$line"`. Note that the `.40` will cause the output to be truncated if it's longer which may be what you want, but if not you can omit that part. – Dennis Williamson Nov 21 '10 at 21:53
2

This is a very old question (2010) but it's the top google result, so might as well. Of the existing answers here, one is a guess that doesn't adjust for terminal width, and the other one invokes sed which is unnecessarily costly.

The printf solution is better as it's a bash builtin, so it vwon't slow things down, but instead of guessing - bash gives you $COLUMNS to tell you how wide the terminal window you're dealing with is.

so while you can explicitly align to, say the 40th column:

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

You can size it for whatever your terminal width is with:

printf "%$COLUMNSs\n" "$the_weather"

(since we're mixing up syntax here, we have used the full form syntax for a bash variable i.e. ${COLUMNS} instead of $COLUMNS, so that bash can identify the variable from the other syntax

In action .. now that we've freed up all that sed processing time, we can use it for something else maybe:

the_weather="$(curl -sm2 'http://wttr.in/Dublin?format=%l:+%c+%f')"
printf "%${COLUMNS}s\n" "${the_weather:-I hope the weather is nice}"
Tadhg
  • 21
  • 1
1

Your final step could be

sed -e :a -e 's/^.\{1,$length\}$/ &/;ta'
Wesley Rice
  • 2,711
  • 19
  • 8
  • when i put this in while: f=`fold -w$lenght $file > output` while read line do sed -e :a -e 's/^.\{1,$length\}$/ &/;ta' echo " after sed, line is $line" done < "test" it gives me error: sed: 1: "s/^.\{1,$length\}$/ &/;ta ": RE error: invalid repetition count(s) after sed, line is LAST LINE – cubrilo Nov 21 '10 at 17:23
  • @cubrilo: Change the single quotes to double quotes: `sed -e :a -e "s/^.\{1,$length\}$/ &/;ta"`, but this way is slow since it loops until the string is long enough. – Dennis Williamson Nov 21 '10 at 21:56