I have a script that runs about 10 other scripts in the background that constantly generate new output. I would like to keep that initial script running and be able to simply type in it to filter all the output displayed on the terminal in real time.
For the sake of simplicity let say each of these for loops represents one of my background scripts (The outputs contain color codes btw.):
for i in {0..1000} ; do
echo -e "\033[0;31mHello world\033[0m $i"
sleep 1
done &
for i in {0..1000} ; do
echo -e "\033[0;34mHello world\033[0m $i"
sleep 1
done &
My first idea was to redirect all the output of the background scripts into a file and then use the read
command to get a search query and pass it to grep
like so:
for i in {0..1000} ; do
echo -e "\033[0;31mHello world\033[0m $i"
sleep 1
done >> ./output.txt 2>&1 &
for j in {0..1000} ; do
echo -e "\033[0;34mHello world\033[0m $j"
sleep 1
done >> ./output.txt 2>&1 &
while true ; do
read -p "Search: " query
clear
cat ./output.txt | grep "$query"
done
But there are several issues with that.
Update problem: The output won't update when the output.txt
file changes. So you have to repeatedly search again to get up-to-date results.
Color code problem: If I search for 3
it will print all lines because the color codes contains a 3
. I could of course completely filter out the colors like so: cat ./output.txt | sed 's/\x1b\[[0-9;]*m//g' | grep "$query"
, but I don't want to lose the color in the final output so it's not that easy.
Disappearing input problem: Even if I'd manage to get new output to be printed in real time after hitting enter for a search, the user won't be able to type (or see, to be more accurate) his/her next search properly. I would like the filter text the user is typing to always be visible.
Any ideas how this could be accomplished?