2012-02-03 33 views
0

如果我運行一個程序,然後它會顯示以下的終端屏幕如何從終端輸出中取出零件?

Total Events Processed         799992 
Events Aborted (part of RBs)         0 
Events Rolled Back           0 
Efficiency            100.00 % 
Total Remote (shared mem) Events Processed     0 
Percent Remote Events          0.00 % 
Total Remote (network) Events Processed      0 
Percent Remote Events          0.00 % 

Total Roll Backs            0 
Primary Roll Backs           0 
Secondary Roll Backs           0 
Fossil Collect Attempts          0 
Total GVT Computations          0 

Net Events Processed         799992 
Event Rate (events/sec)        3987042.0 

如果我想採取第一和第五行從輸出那麼我該怎麼做呢?

+1

編輯您的標籤以指示您是使用Linux/Unix還是Windows或?祝你好運。 – shellter 2012-02-03 04:15:51

回答

1

可以使用grep工具(如果可用):

$ ./program | grep 'Total Events Processed\|Total Remote (shared mem) Events Processed' 
1

我認爲SED會做到這一點,如:

sed -n -e 1p -e 5p input.txt 
0

你可以使用awk做到這一點,太:

awk 'NR==1 || NR==5 {print;} NR==6 {nextfile;}' 

或更漂亮的sed:

sed -ne '1p;5p;6q' 

甚至通過純Bourne shell腳本管道,如果這是你滾(按你的標籤)的方式:

#!/bin/sh 
n=0 
while read line; do 
    n=$((n+1)) 
    if [ $n = 1 -o $n = 5 ]; then 
    echo "$line" 
    elif [ $n = 6 ]; then 
    break 
    fi 
done 

注意,在所有情況下,我們正在以創紀錄的6戒菸,因爲沒有需要繼續瀏覽輸出。