4

次のファイルがあります

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 内には、次の 3 つの列が表示されます。

===========================
| | ライオン | 3 | 1 |
| | カメ | カメ 1 | 0 |
| | モンキー | モンキー 4 | 0 |
| | パンダ | 2 | 1 |
| | フクロウ | フクロウ 5 | 4 |
===========================

レンダリングのヘルプに感謝します。

4

1 に答える 1

6

データの規則性に応じて、次のように置き換えることで、フィールドに直接アドレス指定できます。

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
于 2012-09-10T09:56:11.430 に答える