私たちは、私は次のテキストがあるとしましょう:次のように...、grepの出力にどのようにラベルを付けることができますか?
name is test1 and age is test2 end
name is test3 and age is test4 end
name is test5 and age is test6 end
name is test7 and age is test8 end
私はtest1のためにgrepをしています、TEST2:
-bash$ grep -o -P "is .*? and|is .*? end" test
is test1 and
is test2 end
is test3 and
is test4 end
is test5 and
is test6 end
is test7 and
is test8 end
を私がするいくつかのテキストを付加することができる方法があります一致パターン?私はこのような出力を探しています:
STRING1:is test1 and
STRING2:is test2 end
STRING1:is test3 and
STRING2:is test4 end
STRING1:is test5 and
STRING2:is test6 end
STRING1:is test7 and
STRING2:is test8 end
+1はお時間をいただき、ありがとうございます。実際には、私の唯一の関心事は、正規表現の数が増えた場合、 'sed'ですべてを繰り返す必要があるということですね。それとも私は何かが欠けている?そして、終わりの言葉がはっきりしない場合があります。私が達成しようとしているのは、grepが探しているものを「ラベル付けする」方法を見つけることです。 – Legend
各マッチに任意の文字列がある場合は、すべての解決方法が必要です。しかし、合理的なアルゴリズム関係があれば、awk(またはPythonまたはPerl)スクリプトを書くことができ、すべてを書き出す必要はありません。また、マッチした行そのものが十分なラベルではないのでしょうか? (また、終わりの単語が足りない場合は、 'sed'と' grep'の違いを考慮して、正規表現全体をsedスクリプトに入れてください。) – huon
ありがとうございました!それは役に立ちます。 – Legend