开发者

KornShell- Creating a fixed width text file

开发者 https://www.devze.com 2022-12-11 19:05 出处:网络
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 c

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
0

精彩评论

暂无评论...
验证码 换一张
取 消