2

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!

6
  • 1
    Why don't you embed color codes in your format? Commented Oct 23, 2019 at 9: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. Commented Oct 23, 2019 at 9: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. Commented Oct 23, 2019 at 9:39
  • Thank you, Gordon for your advice, Commented Oct 23, 2019 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" Commented Oct 23, 2019 at 10:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This example will don't work with my case.
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".
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" .
And here's red: readonly const_TextRed='\e[0;31m' .
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" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.