Some programs produce output to bash in a way that the user experience like strings that changes values rather than rows printed(bash scrolls down to be able to show all the changing output). What is this phenomena called? How is it achieved in e.g a java program or a bash script. How can the output be parsed? Is it just printing new lines but using some special function?
top is one program that uses this "phenomena" of outputting data
Okey cursors are used.
Then how can i parse them in java? I tried this code but it does not print anything
public static void exeTest(String [] args) throws IOException{
if (args.length <= 0) {
System.out.println("empty command");
return;
}
Process process = new ProcessBuilder(args).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.printf("Output of running %s is:",
Arrays.toString(args));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Thanks for all good answers, im now more familiar at this topic even thou I still cant parse a program output like the one from top in java. Ill close this question and do more research about how to parse this in java. Then I may start another more specific one about just parsing the output in java.
Luckily you can disable this behavior: Gnome terminal: *Edit -> Profile preferences -> Scrolling", uncheck Scroll on output. Terminator: Right click on terminal screen, Preferences -> Profile -> (for each profile) -> Scrolling, uncheck Scroll on output.
Scrolling on the Output To do this, press the Menu button. Select Preferences. Select your current profile from the Profiles section. Then select Scrolling.
For most users, you should be able to scroll up and down, one line at a time using Shift+UpArrow or Shift+DownArrow. To jump an entire page at a time, try Shift+PageUp or Shift+PageDown. If these commands don't work, it's likely your terminal is using different keybindings.
As an aside, CTRL-ALT PgUp or PgDn will let you scroll one line at a time.
Instead of printing whole lines (as with println
or printing %n
), write backspace characters (\b
) or bare carriage returns without linefeeds (\r
) to move the cursor back in front of what you want to print over.
Since you have tagged the question with bash
, I am presuming that you are running linux/cygwin. Hence it's likely that the terminal is ANSI compatible. In that case, you could also use ANSI escape sequences...
e.g.:
echo -e "Hello world\rHi"
# Produces "Hillo world" since the line is not cleared before printing "Hi".
echo -e "Hello world\r\e[KHi"
#produces "Hi" (prints "Hello world", goes to 1st column, clears to last column, prints "Hi")
# For more details, refer above link.
If you need to run it in loop (which is the most obvious case), make sure that either
NOTE:
This code given above are bash commands. Sorry, I am not much familiar with java syntax, but to my knowledge,
System.out.println
System.out.printf
\e
refers to <ESC>
character (ascii = 27), can be achieved with \033
in java string.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With