1

I have such issue. In one script I have a formatted printf string. Some columns should be marked in different colors, but if try to mark the with colors it destroy whole formatting.

This one is don't work

printf  "%5s|%-6.5s|%-70.69s|%-9.8s|%7.6s|%7.6s|%-15.30s\n" \ "Nr. " " One " "Two" "Three" "Four" ""$(tput setaf 1)"Five"$(tput sgr0)"" " Six"

Without colors works:

printf  "%5s|%-6.5s|%-70.69s|%-9.8s|%7.6s|%7.6s|%-15.30s\n" \ "Nr. " " One " "Two" "Three" "Four" "Five" " Six"

Has anybody a solution?

Thanks in advance!

user72394
  • 101
  • 1
  • 1
  • 6
  • 1
    Why don't you embed color codes in your format? – oguz ismail Oct 23 '19 at 09:26
  • 2
    The format string `%7.6s` is clipping it to only 6 characters; the color codes are normally 5 characters (each), so you get the opening color code and one character of printable content. Either put the color codes in the format string as @oguzismail suggested), or put them in separate fields with no length limit. BTW, your quoting on the color-coded argument is also weird. – Gordon Davisson Oct 23 '19 at 09:33
  • The command `printf` consists of a _format string_ and an _argument list_ which printed according to the format. You should see colours as part of the format-string and hence, they should belong in the format string. – kvantour Oct 23 '19 at 09:39
  • Thank you, Gordon for your advice, – user72394 Oct 23 '19 at 10:28
  • printf "%5s|%-6.5s|%-70.69s|%-9.8s|%7.6s|"$(tput setaf 1)"%7.6s"$(tput sgr0)"|%-15.30s\n" \ "Nr. " " One " "Two" "Three" "Four" "Five" " Six" – user72394 Oct 23 '19 at 10:30
  • @user, no need to unquote the `$(tput ...)` Just do: `printf "%5s|%-6.5s|%-70.69s|%-9.8s|%7.6s|$(tput setaf 1)%7.6s$(tput sgr0)|%-15.30s\n" \ "Nr. " " One " "Two" "Three" "Four" "Five" " Six"` – kvantour Oct 23 '19 at 10:36

2 Answers2

1

Hmmm... what you are doing is a bit odd I suppose. Looks like you have seven format strings you want to iterate to seven text blocks. Ok. I used a different method for text coloring when I wanted colored text in a script.

const_TextPlain='\e[0m' const_TextYellow='\e[1;33m' printf '%b' "${const_TextYellow}" "${important_message}: " "${const_TextPlain}" '\n'

You'd want to include format strings for your text mods so as to prevent the mangling. So nine format strings instead of seven. I'd look at the comments to your post as well.

JamesIsIn
  • 181
  • 1
  • 6
  • This example will don't work with my case. – user72394 Oct 23 '19 at 10:22
  • Sure it will: `const_TextPlain='\e[0m' ; const_TextYellow='\e[1;33m' ; printf "%5s|%-6.5s|%-70.69s|%-9.8s|%7.6s|"${const_TextYellow}"%7.6s"${const_TextPlain}"|%-15.30s\n" \ "Nr. " " One " "Two" "Three" "Four" "Five" " Six"`. – JamesIsIn Oct 23 '19 at 10:56
  • Or maybe you think it won't work this way? `printf "%5s|%-6.5s|%-70.69s|%-9.8s|%7.6s|%b%7.6s%b|%-15.30s\n" \ "Nr. " " One " "Two" "Three" "Four" "${const_TextYellow}" "Five" "${const_TextPlain}" " Six" `. – JamesIsIn Oct 23 '19 at 11:01
  • And here's red: `readonly const_TextRed='\e[0;31m' `. – JamesIsIn Oct 23 '19 at 11:05
1

This works:

printf  "%5s|%-6.5s|%-70.69s|%-9.8s|%7.6s|"$(tput setaf 1)"%7.6s"$(tput sgr0)"|%-15.30s\n" \ "Nr. " " One " "Two" "Three" "Four" "Five" " Six"
user72394
  • 101
  • 1
  • 1
  • 6