English 中文(简体)
KornShell Printf - Padding a string
原标题:

I m attempting to write a KornShell (ksh) function that uses printf to pad a string to a certain width.

Examples:

Call

padSpaces Hello 10

Output

 Hello      

I currently have:

padSpaces(){
        WIDTH=$2
        FORMAT="%-${WIDTH}.${WIDTH}s"
        printf $FORMAT $1
}

Edit: This seems to be working, in and of itself, but when I assign this in the script it seems to lose all but the first space.

TEXT=`padSpaces "TEST" 10`
TEXT="${TEXT}A"
echo ${TEXT}

Output:

TEST A

I m also open to suggestions that don t use printf. What I m really trying to get at is a way to make a fixed width file from ksh.

最佳回答

Your function works fine for me. Your assignment won t work with spaces around the equal sign. It should be:

SOME_STRING=$(padSpaces TEST 10)

I took the liberty of replacing the backticks, too.

You don t show how you are using the variable or how you obtain the output you showed. However, your problem may be that you need to quote your variables. Here s a demonstration:

$ SOME_STRING=$(padSpaces TEST 10)
$ sq= 
$ echo $sq$SOME_STRING$sq
 TEST  
$ echo "$sq$SOME_STRING$sq"
 TEST       
问题回答

Are you aware that you define a function called padSpaces, yet call one named padString? Anyway, try this:

padString() {
    WIDTH=$2
    FORMAT="%-${WIDTH}s"
    printf $FORMAT $1
}

Or, the more compact:

padString() {
    printf "%-${2}s" $1
}

The minus sign tells printf to left align (instead of the default right alignment). As the manpage states about the command printf format [ arg ... ],

The arguments arg are printed on standard output in accordance with the ANSI-C formatting rules associated with the format string format.

(I just installed ksh to test this code; it works on my machineTM.)





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签