How to extract some fields from real-time output of a command in a bash script

I want to extract some fields from the output of the xentop . This is like the top command; provides a constant look at processor usage, memory usage ... in real time. If I run this command in batch mode, I will have its output, as you see in the file:

  NAME STATE CPU(sec) CPU(%) MEM(k) MEM(%) MAXMEM(k) MAXMEM(%) VCPUS NETS NETTX(k) NETRX(k) VBDS VBD_OO VBD_RD VBD_WR VBD_RSECT VBD_WSECT SSID Domain-0 -----r 13700 33.0 7127040 85.9 no limit n/a 8 0 0 0 0 0 0 0 0 0 0 fed18 -----r 738 190.6 1052640 12.7 1052672 12.7 3 1 259919 8265 1 0 82432 22750 2740966 1071672 0 

and running this

 cat file| tr '\r' '\n' | sed 's/[0-9][;][0-9][0-9][aZ]/ /g' | col -bx | awk '{print $1,$4,$6}' 

in this file gives me what i want

 NAME CPU(%) MEM(%) Domain-0 33.0 85.9 fed18 190.6 12.7 

but my script does not work on xentop output in real time. I even tried to just start xentop once, setting the iteration parameter to 1 ( xentop -i 1 ), but that xentop -i 1 work! How can I pass the xentop output as "not" in real time to my script?

+4
source share
2 answers

It cannot send any output to standard output. There are several ways to send screen output without using stdout. A quick Google search did not provide much information on how it works internally.

0
source

I am using xentop version 1.0 on xenserver 7.0, for example:

 [ root@xen ] xentop -V xentop 1.0 [ root@xen ] cat /etc/centos-release XenServer release 7.0.0-125380c (xenenterprise) 

If you want to save xentop output, you can do this with the options '-b' (batch mode) and '-i' (number of iterations before exiting):

 [ root@xen ] xentop -b -i 1 NAME STATE CPU(sec) CPU(%) MEM(k) MEM(%) MAXMEM(k) MAXMEM(%) VCPUS NETS NETTX(k) NETRX(k) VBDS VBD_OO VBD_RD VBD_WR VBD_RSECT VBD_WSECT SSID Domain-0 -----r 132130 0.0 4194304 1.6 4194304 1.6 16 0 0 0 0 0 0 0 0 0 0 MY_VM --b--- 5652 0.0 16777208 6.3 16915456 6.3 4 0 0 0 1 - - - - - 0 [ root@xen ] xentop -b -i 1 > output.txt [root @xen ] cat output.txt NAME STATE CPU(sec) CPU(%) MEM(k) MEM(%) MAXMEM(k) MAXMEM(%) VCPUS NETS NETTX(k) NETRX(k) VBDS VBD_OO VBD_RD VBD_WR VBD_RSECT VBD_WSECT SSID Domain-0 -----r 132130 0.0 4194304 1.6 4194304 1.6 16 0 0 0 0 0 0 0 0 0 0 MY_VM --b--- 5652 0.0 16777208 6.3 16915456 6.3 4 0 0 0 1 - - - - - 0 
0
source

Source: https://habr.com/ru/post/1501066/


All Articles