KornShell- Creating a fixed width text file
I need to create a simple fixed width text file in KornSh开发者_运维技巧ell (ksh). My current attempt using printf to pad the string isn't working out very well. What's the shortest, cleanest way to create a fixed width string in ksh?
KSH compresses several spaces into one when it parses certain inputs. So to achieve what you want, you must write the formatted string directly to a file without passing it through any variables. Use printf
to format everything in one go and redirect to the file:
printf "%-10s%-5s%-20s\n" $str1 $str2 $str3 >> file
As I stated in my answer to that question, you need to put quotes around your variables.
TEXT=`padSpaces "TEST" 10`
TEXT="${TEXT}A"
echo ${TEXT}
TEST A
echo "${TEXT}"
TEST A
精彩评论