Continuous grep, output at same spot on console - linux

I use
tail -f file | grep pattern
all the time for continuous grep.
However, is there a way I can make grep output its pattern at the same spot, say at the top of the screen? so that the screen doesn't scroll all the time?
My case is something like this: tail -f log_file | grep Status -A 2 will show the current status and what changed it to that status. The problem is the screen scrolls and it becomes annoying. I'd rather have the output stuck on the first 3 lines in the screen.
Thank you!

you could use the watch command; which will always execute the same command, but the position on the screen will stay the same. The process might eat some more CPU or memory though:
watch "tail file | grep pattern"
by default watch executes that command every 2 seconds. You can adjust up to 0.1 seconds using:
watch -n 0.1
NOTE
As noted by #etanReisner: this is not exactly the same as tail -f: tail -f will change immediately if something is added to your logfile, the watch command will only notice that when it executes, ie every 2 (or 0.1 seconds).

Assuming you are using a vt100 compatible emulator...
This command will tail a file, pipe it into grep, read it a line at a time and then display it in reverse on the top line of the screen:
TOSL=$(tput sc;tput cup 0 0;tput rev;tput el)
FROMSL=$(tput sgr0; tput rc)
tail -f file | grep --line-buffered pattern | while read line
do
echo -n "$TOSL${line}$FROMSL"
done
It assumes your output appears a line at a time. If you want more than one line, you can read more than a line, but you have to decide how you want to buffer the output. You could also use the csr terminfo command to set up an entire separate scrolling region instead of just having one line.
Here is the scrolling region version with a ten line status area at the top:
TOSL=$(tput sc; tput csr 0 10; tput cup 10 0;tput rev;tput el)
FROMSL=$(tput sgr0; tput rc;tput csr 10 50;tput rc)
tail -f file | grep --line-buffered pattern | while read line
do
echo -n "$TOSL${line}
$FROMSL"
done
Note that it is not impossible that your display will be corrupted from time-to-time as it could be that the output from your main shell and your background task get mixed up.

Simply replace the newlines with carriage returns.
tail -f file | grep --line-buffered whatever | tr '\012' '\015'
The line buffering is to avoid jumpy output; see http://mywiki.wooledge.org/BashFAQ/009
This is quick and dirty. As noted in comments, this will leave the previous contents of the line underneath, so a shorter line will not completely overlay a longer line. You could add some control codes to address that, but then you might as well use Curses for the formatting too, like in rghome's answer.

Related

How to overwrite previous output in bash

I have a bash script, that outputs top most CPU intensive processes every second to the terminal.
tmp=$(ps -e -eo pid,cmd,%mem,%cpu,user --sort=-%cpu | head -n 11)
printf "\n%s\n" "$tmp[pid]"
I know that I can move my cursor to the predeclared position, but that fails every time terminal is not cleared.
I could also just go to the beginning of the line and write over it, but that again makes a problem when current output is shorter that the previous and when the number of lines is not the same as it was at the previous output.
Is there a way to completely erase the previous output and write from there?
Yes, you can clear a part of the screen before each iteration (see https://unix.stackexchange.com/questions/297502/clear-half-of-the-screen-from-the-command-line), but the function watch does it for you. Try:
watch -n 1 "ps -e -eo pid,cmd,%mem,%cpu,user --sort=-%cpu | head -n 11"

Different behaviour in bash when printing output from command compared to hardcoded string

I am trying to use tmux and simple bash scripts to get a very basic statusbar in my terminals - so the windows are very small (1 or 2 lines in height and potentially less than 10 characters in width).
The command that's causing a strange behaviour is:
echo "$(top -bn 1 | awk '/^%Cpu/{print $2}')%"
which is basically just supposed to print out something like 1.8%.
In a normal sized terminal (with or without tmux) it works fine but when I shrink down the terminal, at some point it starts to cut off the number and only displays %. This might not seem too strange but when I put some text around this command, like
echo "ab$(top -bn 1 | awk '/^%Cpu/{print $2}'cd"
I get abcd as output when shrinking the terminal. So it's only affecting the part withing $(...) that gets cut away.
Also when I print two lines:
echo "$(top -bn 1 | awk '/^%Cpu/{print $2}'%"
echo "20.5%"
it will print 20.5% fine, even break it up over several lines like
2
0
.
5
%
rather than just not printing parts of it. The first line however only shows % without any number.
Any idea what is causing this and how to get the numbers to show?
By default, top checks the terminal size and adjusts its output based on it, even when used in batch mode and output is being piped instead of going to the terminal. You can use the -w option to override this.
echo "$(top -w 50 -bn 1 | awk '/^%Cpu/{print $2}')%"

tail -f <filename>, print line number as well

Is there a way to modify so that the tail -f lists the line number of the current file as well.
Something similar to grep -n <Strings> *.
Try less
Instead of using tail to follow data and less or nl for numbering, I suggest using a single tool for both:
less -N +F <filename>
This will make less print line numbers and follow the file. From man less:
F
Scroll forward, and keep trying to read when the end of file is reached. Normally this command would be used when already at the end of the file. It is a way to monitor the tail of a file which is growing while it is being viewed. (The behavior is similar to the tail -f command.)
You could do a Ctrl+C to stop following when inside less; to start following again, you could press F again. With this method, you get the additional goodies that less offers like regex-based search, tags, etc.
This command takes into account the number of lines above also
tail -f -n +1 yourfile.txt | nl
You can use less command,
tail -f <filename> | less -N
According to man page of less
-N or --LINE-NUMBERS
Causes a line number to be displayed at the beginning of each
line in the display.

bash when inside screen, output is truncated if does not fit in a line

Outside screen -
[j#firedesire rb]$ diff -bu root_es-US.pres root_es-MX.pres | sed -n "s/^-\([^=]*\)=.*/'\1'/p" | tr "\\n" ","
'IMG.HEAD.LOGO.URL','LNK.COPYRIGHT.URL','LNK.FOOTSECURITY.URL','LNK.HEADHELP.URL','LNK.HEADYAHOO.URL','LNK.PRIVACY.URL','LNK.TOS.URL','STR.HEAD.LOGO.HEIGHT','STR.HEAD.LOGO.WIDTH','REG.TOS.MAIL.URL','UPGRADE.STR.AGREE.STATEMENT',[j#firedesire rb]$
Inside screen -
[j#firedesire rb]$ diff -bu root_es-US.pres root_es-MX.pres | sed -n "s/^-\([^=]*\)=.*/'\1'/p" | tr "\\n" ","
'IMG.HEAD.LOGO.URL','LNK.COPYRIGHT.URL','LNK.FOOTSECURITY.URL','LNK.HEADHELP.URL','LNK.HEADYAHOO.URL','LNK.PRIVACY.URL','LNK.TOS.URL','STR.HEAD.LOGO.HEIGHT','STR.HEAD.LOGO.WIDTH','REG.TOS.MAIL.URL','UPGR
It gets cut off at 'UPGR where line ends. What could be causing this problem, how can I fix this ?
Your output is wider than your screen
You can force the output to wrap by piping it through less -+S, you'll have to use your arrow keys to view all of the output.
There's an option in screen to toggle wrapping on or off; you may have luck with that as well: https://www.gnu.org/software/screen/manual/html_node/Wrap.html
Another method besides less is to pipe it through cat:
ps aux | cat
top | cat
That won't pause at every screenful, like less or more would do, but when programs would otherwise halt until it receives user input, when they detect that their output is being piped to another process, they just dump out the results and keep going.
I use this a lot with external diff programs attached to my git or svn flags: git diff | cat keeps feeding my diff program all the extra files after the first one, while git diff by itself doesn't.

How can you read the most recent line from the linux program screen?

I use screen to run a minecraft server .jar, and I would like to write a bash script to see if the most recent line has changed every five minutes or so. If it has, then the script would start from the beginning and make the check again in another five minutes. If not, it should kill the java process.
How would I go about getting the last line of text from a screen via a bash script?
If I have understand, you can redirect the output of your program in a file and work on it, with the operator >.
Try to run :
ls -l > myoutput.txt
and open the file created.
You want to use the tail command. tail -n 1 will give you the last line of the file or redirected standard output, while tail -f will keep the tail program going until you cancel it yourself.
For example:
echo -e "Jello\nPudding\nSkittles" | tail -n 1 | if grep -q Skittles ; then echo yes; fi
The first section simply prints three lines of text:
Jello
Pudding
Skittles
The tail -n 1 finds the last line of text ("Skittles") and passes that to the next section.
grep -q simply returns TRUE if your pattern was found or FALSE if not, without actually dumping or outputting anything to screen.
So the if grep -q Skittles section will check the result of that grep -q Skittles pattern and, if it found Skittles, prints 'yes' to the screen. If not, nothing gets printed (try replacing Skittles with Pudding, and even though it was in the original input, it never made it out the other end of the tail -n 1 call).
Maybe you can use that logic and output your .jar to standard output, then search that output every 5 minutes?

Resources