Bash

從命令輸出中刪除行

  • April 16, 2019

我在命令下執行;我需要將命令的輸出儲存在一個文件中,但沒有標題。

我試過 - yarn top | tail -n +8yarn top就像 Linux 一樣top,它每 3 秒刷新一次並重複輸出)

yarn top
YARN top - 00:47:26, up 24d, 2:49, 0 active users, queue(s): root
NodeManager(s): 7 total, 7 active, 0 unhealthy, 0 decommissioned, 0 lost, 0 rebooted
Queue(s) Applications: 8 running, 111 submitted, 15 pending, 111 completed, 5 killed, 5 failed
Queue(s) Mem(GB): 100 available, 20 allocated, 0 pending, 0 reserved
Queue(s) VCores: 200 available, 2 allocated, 0 pending, 0 reserved

                 APPLICATIONID USER             TYPE      QUEUE   #CONT  #RCONT  VCORES RVCORES     MEM    RMEM  VCORESECS    MEMSECS %PROGR       TIME NAME
application_1484661449412_1115434 papp     mapreduce    default       3       0       3       0     16G      0G         28        172   5.00   00:00:00 EdmHdpIf-SRI
application_1484661449412_1115420 papp     mapreduce    default       2       0       2       0     12G      0G         86        545  95.00   00:00:00 oozie:launch
application_1484661449412_1115433 eapp     mapreduce       eapp       2       0       2       0     12G      0G         23        160   5.00   00:00:00 oozie:launch
application_1484661449412_1115386 eapp     mapreduce       eapp       2       0       2       0     12G      0G        250       1524  95.00   00:00:02 oozie:launch

預期產出

application_1484661449412_1115434 papp     mapreduce    default       3       0       3       0     16G      0G         28        172   5.00   00:00:00 EdmHdpIf-SRI
application_1484661449412_1115420 papp     mapreduce    default       2       0       2       0     12G      0G         86        545  95.00   00:00:00 oozie:launch
application_1484661449412_1115433 eapp     mapreduce       eapp       2       0       2       0     12G      0G         23        160   5.00   00:00:00 oozie:launch
application_1484661449412_1115386 eapp     mapreduce       eapp       2       0       2       0     12G      0G        250       1524  95.00   00:00:02 oozie:launch

在這種情況下,使用 grep 效果最好:

1) yarn top | grep -A20 APPLICATIONID

A - 匹配字元串後列印數據

20 - 在 (A) 匹配字元串之後要列印的行數

[gc13@oc1245342277 sh]$ cat text1 
YARN top - 00:47:26, up 24d, 2:49, 0 active users, queue(s): root
NodeManager(s): 7 total, 7 active, 0 unhealthy, 0 decommissioned, 0 lost, 0 rebooted
Queue(s) Applications: 8 running, 111 submitted, 15 pending, 111 completed, 5 killed, 5 failed
Queue(s) Mem(GB): 100 available, 20 allocated, 0 pending, 0 reserved
Queue(s) VCores: 200 available, 2 allocated, 0 pending, 0 reserved

                 APPLICATIONID USER             TYPE      QUEUE   #CONT  #RCONT  VCORES RVCORES     MEM    RMEM  VCORESECS    MEMSECS %PROGR       TIME NAME
application_1484661449412_1115434 papp     mapreduce    default       3       0       3       0     16G      0G         28        172   5.00   00:00:00 EdmHdpIf-SRI
application_1484661449412_1115420 papp     mapreduce    default       2       0       2       0     12G      0G         86        545  95.00   00:00:00 oozie:launch
application_1484661449412_1115433 eapp     mapreduce       eapp       2       0       2       0     12G      0G         23        160   5.00   00:00:00 oozie:launch
application_1484661449412_1115386 eapp     mapreduce       eapp       2       0       2       0     12G      0G        250       1524  95.00   00:00:02 oozie:launch
[gc13@oc1245342277 sh]$ 
[gc13@oc1245342277 sh]$ 
[gc13@oc1245342277 sh]$ 
[gc13@oc1245342277 sh]$ 
[gc13@oc1245342277 sh]$ cat text1 | grep -A20 APPLICATIONID
                 APPLICATIONID USER             TYPE      QUEUE   #CONT  #RCONT  VCORES RVCORES     MEM    RMEM  VCORESECS    MEMSECS %PROGR       TIME NAME
application_1484661449412_1115434 papp     mapreduce    default       3       0       3       0     16G      0G         28        172   5.00   00:00:00 EdmHdpIf-SRI
application_1484661449412_1115420 papp     mapreduce    default       2       0       2       0     12G      0G         86        545  95.00   00:00:00 oozie:launch
application_1484661449412_1115433 eapp     mapreduce       eapp       2       0       2       0     12G      0G         23        160   5.00   00:00:00 oozie:launch
application_1484661449412_1115386 eapp     mapreduce       eapp       2       0       2       0     12G      0G        250       1524  95.00   00:00:02 oozie:launch
[gc13@oc1245342277 sh]$ 

引用自:https://unix.stackexchange.com/questions/344062