最初の「イエロー ライオン」の前に「アニマル キング 1:」、2 番目の「イエロー ライオン」の前に「アニマル キング 2:」、前に「アニマル キング 3:」を出力する小さな sed スクリプトがあります。 3番目の「イエローライオン」の発生。これをsedで達成することは可能ですか?
sed および出力の入力ファイルの内容は、次の場所で確認できます。
[user@T60 ~]$ cat animals.txt
red cat
blue dog
yellow lion
brown tiger
black hippo
yellow lion
white zebra
yellow lion
[martint@martint ~]$ cat sed-test.sh
#!/usr/bin/env bash
sed 's/\(.*lion\)/\
\
animal king 1:\
\
\1 /' < "animals.txt"
[user@T60 ~]$ ./sed-test.sh
red cat
blue dog
animal king 1:
yellow lion
brown tiger
black hippo
animal king 1:
yellow lion
white zebra
animal king 1:
yellow lion
[user@T60 ~]$
ご覧のとおり、「動物の王」の数は 3 回すべてで「1」です。sed で ".*lion" が一致するたびにこれを 1 ずつ増やす方法はありますか?