2012-09-10 63 views
3

我有以下文件grep信息並保存爲可讀取的Excel文件?

There is a group of Lion in the forest. There are 3 active. There are 1 sleeping 

    Lion1 is talking to Lion2 
    Lion2 is talking to Lion3 
    Lion3 is touching Lion1 

There is a group of Turtle in the forest. There are 1 active. There are 0 sleeping 

There is a group of Monkey in the forest. There are 4 active. There are 0 sleeping 

    Monkey1 is talking to Monkey3 
    Monkey4 is jumping about 

There is a group of Panda in the forest. There are 2 active. There are 1 sleeping 

There is a group of Owl in the forest. There are 5 active. There are 4 sleeping 

我用下面的命令:

grep "There is a group" | awk '{print substr($0,10)}' 

,我得到以下

Lion in the forest. There are 3 active. There are 1 sleeping 
Turtle in the forest. There are 1 active. There are 0 sleeping 
Monkey in the forest. There are 4 active. There are 0 sleeping 
Panda in the forest. There are 2 active. There are 1 sleeping 
Owl in the forest. There are 5 active. There are 4 sleeping 

問題

我應該怎麼修改我的grep和awk命令,以便得到以下結果:

Lion, 3, 1 
Turtle, 1, 0 
Monkey, 4, 0 
Panda, 2, 1 
Owl, 5, 4 

結果將存儲到將由Microsoft Excel讀取的文件中。在Microsoft Excel內部,我將能夠看到如下三列:

 
=========================== 
| Lion  | 3 | 1 | 
| Turtle  | 1 | 0 | 
| Monkey  | 4 | 0 | 
| Panda  | 2 | 1 | 
| Owl  | 5 | 4 | 
=========================== 

欣賞任何幫助渲染。

回答

5

根據數據的規律性,你可以只直接通過更換地址字段:

grep "There is a group" | awk '{print substr($0,10)}' 

awk -v OFS=', ' '/There is a group/ { print $6, $12, $16 }' 

輸出:

Lion, 3, 1 
Turtle, 1, 0 
Monkey, 4, 0 
Panda, 2, 1 
Owl, 5, 4 
+0

謝謝回答。我已經編輯了你的答案,使其更加清晰 – Jack

+0

傑克,你把'grep'放回了那裏,但當你已經在'awk'中匹配模式時,你就不再需要它了。 – wich

+0

@wich。謝謝你的提示。我已經對答案做了一些修改。 – Jack