Bash Leftpad String with Spaces Inside Variable

Bash LeftPad String with spaces inside variable

To prevent word expansion, double quote the variable:

printf "| %-30s " "$CUSTOMERNAME"

How do I print some text in bash and pad it with spaces to a certain width?

Use - to left align a field.

printf "Echoing random number %-5s   [ OK ]" $RAND_NUM

Alternatively, if you're on a Red Hat Linux system there are predefined functions that will print out green OK and red FAILED prompts (the ones you see during bootup):

#!/bin/bash

. /etc/init.d/functions

echo -n "Frobbing widget:"
frob_widget && echo_success || echo_failure
echo

Padding zeros in a string

Use backticks to assign the result of the printf command (``):

n=1
wget http://aolradio.podcast.aol.com/sn/SN-`printf %03d $n`.mp3

EDIT: Note that i removed one line which was not really necessary.
If you want to assign the output of 'printf %...' to n, you could
use

n=`printf %03d $n`

and after that, use the $n variable substitution you used before.

Pad a string to a certain length with a chosen character (or hexcode) in Bash?

For strings without spaces

Use printf to pad with spaces, then replace the spaces with a symbol of your choice. Some examples:

printf %10s AABB | tr ' ' X prints XXXXXXAABB.

printf %-10s AABB | tr ' ' X prints AABBXXXXXX.

To insert non-printable symbols instead of X, you can pass an octal escape sequence to tr. printf can convert hexadecimal numbers into octal ones:

printf %10s AABB | tr ' ' \\$(printf %o 0x1f) prints the bytes 1f 1f 1f 1f 1f 1f 41 41 42 42 (can be confirmed by piping through od -tx1 -An).

For strings with spaces

str=AABB
yes "" | head -n $((10-"${#str}")) | tr \\n X
printf %s "$str"

Swap the last two lines to insert padding at the right (like %-10s). Just like before, you can replace X with \\$(printf %o 0x1f) to insert non-printable characters.

How to zero pad a sequence of integers in bash so that all have the same width?

In your specific case though it's probably easiest to use the -f flag to seq to get it to format the numbers as it outputs the list. For example:

for i in $(seq -f "%05g" 10 15)
do
echo $i
done

will produce the following output:

00010
00011
00012
00013
00014
00015

More generally, bash has printf as a built-in so you can pad output with zeroes as follows:

$ i=99
$ printf "%05d\n" $i
00099

You can use the -v flag to store the output in another variable:

$ i=99
$ printf -v j "%05d" $i
$ echo $j
00099

Notice that printf supports a slightly different format to seq so you need to use %05d instead of %05g.

How can I pad a string with a variable number of leading zeros?

You will need to utilize a couple Expressions to handle this.

  1. Declare a String variable to store the input (e.g. named InputVar). You will need to continue using the concat() Expression here with the input value to add the maximum possible zeros required for each item (e.g. 4 for BoxNo and 5 for ItemID).

    This is obviously inefficient, but there's no better way to insert a dynamic number of characters in Flow, to my knowledge.

  2. Declare a second Integer variable to determine the length (e.g. named InputVarLength), with the following custom Expression as the value:

     length(variables('InputVar'))
  3. Finally, declare a third variable that will calculate a substring result (e.g. named InputVarResult). Use this custom substring Expression as the value:

     substring(variables('InputVar'),sub(variables('InputVarLength'),5),5)

For the ItemID or other results, you'd replace the 5s in the substring Expression with the appropriate startIndex and length to return the size you would like. For reference, the substring format is:

substring(text, startIndex, length)

Which includes the String value you want to find a substring of, the position within that string that you want to start from, and how many characters from that starting position you want to include in your substring result.

The nested subtract format is:

sub(minuend, subtrahend)

Which includes the Integer value to be subtracted from (minuend) and the Integer value that you want to subtract (subtrahend). E.g. sub(10, 2) would return an Integer value of 8.

Zero-padding a string/file name in shell script

Use printf, just like in C. It's available in any POSIXly Bourne Shell (zsh, ksh, bash, ...)

$ FILE=$(printf %04d 42).txt
$ echo "$FILE"
0042.txt


Related Topics



Leave a reply



Submit