Sample Header Ad - 728x90

How to preserve updating output in pipe, e.g. when output has `\r`

1 vote
1 answer
31 views
Whenever I pipe some output that is updating (say with \r), e.g. to a pager (less), or to colourise parts of it, the pipe seems to drop the lines that are updating. Any idea how to preserve the updating behaviour while do whatever else I want to do? MWE:
(
  echo num1;
  for i in {01..99}; do
    printf "num2: %s\r" $i;
    sleep 0.5;
  done
)
Normally the above shows:
num1
num2: NN
where NN counts from 01 to 99. But if I pipe the above to less, I only see the line num1, or say I pipe it to sed to make all occurences of num bold like this:
(
  ...
) | sed -e "s/num/$(tput bold)&$(tput sgr0)/g"
I only see "**num**1", the line with "num2" disappears. How can I keep the second line?
Asked by suvayu (162 rep)
Jan 6, 2024, 01:18 AM
Last activity: Oct 6, 2024, 12:19 PM