I am developping a script that will allow me to find easily which columns are "only spaces", by creating a mask showing any column with anything other than a space.
To do this: I print each line "on top of each others" + modify spaces to "right-arrow". (an additional step would also be to ignore spaces seen after a "column farther than the beginning of the last title", but it is out of topic here)
I have trouble with the last step: how to get the resulting string (the output my terminal displays correctly), without every \r, \n, and <Esc>[C
(= right arrow) that were used to create/interpret it.
Exemple:
$ PS2=""
$ Esc=$( printf '\033' )
$ Right=$( printf "${Esc}[C" )
$ ps | head -n 2
PID TTY TIME CMD
12415 pts/1160 00:00:00 bash
$ ps | head -n 2 | LC_ALL=C tr '!-~' '*'
*** *** **** ***
***** ******** ******** ****
$ ps | head -n 2 | LC_ALL=C tr '!-~' '*' | sed -e "s/ /${Right}/g" | while read line; do
printf "%s\r" "$line"
done ; printf '\n'
***** ******** ******** ****
## the line above is what I am looking for:
## a "mask" of each column that at one point had a non-space character displayed in it
$ ( ps | head -n 2 | tr '!-~' '*' | sed -e "s/ /${Right}/g" | while read line; do
printf "%s\r" "$line"
done ; printf '\n'
) | cat -ve
^[[C^[[C***^[[C***^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C****^[[C***^M*****^[[C********^[[C********^[[C****^M$
## but of course, the terminal sees (and outputs) this instead, which contains all the "contructing characters"
How could I retrieve in a variable the string "as shown by my terminal", ie:
***** ******** ******** ****
(I tried to output that whole thing into a "while read -e finalstring; do print '[%s]' "$finalstring" ; done , but that finalstring still contains the whole "pre-interpretation" string, not the one "post-terminal interpretation")
A shorted way to ask the same question:
# if:
$ printf "aaaa\rbb\rc\n"
# displays:
cbaa
$ var=$( printf "aaaa\rbb\rc\n" ) # $var will be: "aaaa\rbb\rc\n"
# how can I instead have the resulting "displayed string": "cbaa" in $var ?