C. Ross
C. Ross

Reputation: 31848

KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (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?

Upvotes: 1

Views: 3640

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360325

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

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328724

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

Upvotes: 2

Related Questions