(複数行中)最初に一致する部分文字列を取得するには?

(複数行中)最初に一致する部分文字列を取得するには?

次の内容を含むファイルがあります。

/hello="somestuffsomestuffsomestuffsomestuffsomestuffsomestuffsomestuff
       somestuffsomestuffsomestuffsomestuffsomestuffsomestuffsomestuff
       somestuffsomestuffsomestuffsomestuffsomestuffsomestuffsomestuff"
/hello="morestuffmorestuffmorestuffmorestuffmorestuffmorestuffmorestuff
       morestuffmorestuffmorestuffmorestuffmorestuff"

どのように定義を得ることができますか?

/hello="somestuffsomestuffsomestuffsomestuffsomestuffsomestuffsomestuff
       somestuffsomestuffsomestuffsomestuffsomestuffsomestuffsomestuff
       somestuffsomestuffsomestuffsomestuffsomestuffsomestuffsomestuff"

sed私はパイプを通して "/"で区切られた/helloコンテンツを受け取りますが、cut最初の部分文字列を取得する方法がわかりません。

私が使ったコマンド

sed -n '/hello/, /\"/ p' file.txt | cut -d "/" -f 2

何らかの理由で、各部分文字列を最初の行と残りの行に分割します。

ベストアンサー1

次のいずれかを使用してください。

sed -n '/hello/,/"$/p;/"$/q' infile

または、この式は次のようになります。

sed '/hello/,/"$/!d;/"$/q' infile

または、awk次のように使用することもできます(常に最初の行があると仮定hello)。

awk '/hello/ || 1;/"$/{exit}' infile

それ以外の場合は、フラグを使用して管理してください。

awk '/hello/{prnt=1};prnt;/"$/{exit}' infile

おすすめ記事