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 … Read more

sprintf – repeating arguments

It’s all in the documentation! $str=”Str 1: %1$s – Str 2: %2$s – Str 2 again: %2$s”; echo sprintf($str, “I’m string 1”, “My name is string 2”); Note: Use single quotes for the format string otherwise you’ll get PHP Notice: Undefined variable: s in /path/to/tofile:line You can also escape the $ with a \ if … Read more

‘printf’ with leading zeros in C

Your format specifier is incorrect. From the printf() man page on my machine: 0 A zero ‘0‘ character indicating that zero-padding should be used rather than blank-padding. A ‘-‘ overrides a ‘0‘ if both are used; Field Width: An optional digit string specifying a field width; if the output string has fewer characters than the … Read more

Set variable text column width in printf

You can do this as follows: printf(“%*d”, width, value); From Lee’s comment: You can also use a * for the precision: printf(“%*.*f”, width, precision, value); Note that both width and precision must have type int as expected by printf for the * arguments, type size_t is inappropriate as it may have a different size and … Read more

Arduino sprintf float not formatting

Due to some performance reasons %f is not included in the Arduino’s implementation of sprintf(). A better option would be to use dtostrf() – you convert the floating point value to a C-style string, Method signature looks like: char *dtostrf(double val, signed char width, unsigned char prec, char *s) Use this method to convert it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)