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

3 Answers 3

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
3
  • this give me some strange output, don't know why
    – cubrilo
    Nov 21, 2010 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. Nov 21, 2010 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. Nov 21, 2010 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}"
1

Your final step could be

sed -e :a -e 's/^.\{1,$length\}$/ &/;ta'
2
  • 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, 2010 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. Nov 21, 2010 at 21:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.