grepを使用するか、シェルスクリプトを使用する他のオプションを使用してテキストを一覧表示したいと思います。

grepを使用するか、シェルスクリプトを使用する他のオプションを使用してテキストを一覧表示したいと思います。

サブフォルダがrules/resourcesあるためというフォルダがありますA。各サブフォルダーに。BCconstraint.yaml

これで文字列を含むファイルが欲しいですgrep。私は次のように使用しようとしています:constraint.yamlassetTypegrep

grep -rIih assetType rules/resources/

私は次のような結果を得ます。

assetType: cloudfunctions.googleapis.com/CloudFunction
assetType: cloudfunctions.googleapis.com/CloudFunction
assetType: cloudfunctions.googleapis.com/CloudFunction
assetType: cloudfunctions.googleapis.com/CloudFunction
assetType: cloudfunctions.googleapis.com/CloudFunction
assetType: bigquery.googleapis.com/Dataset
assetType: artifactregistry.googleapis.com/Repository
assetType: composer.googleapis.com/Environment
assetType: composer.googleapis.com/Environment
assetType: composer.googleapis.com/Environment
assetType: apigateway.googleapis.com/Gateway
assetType: apigateway.googleapis.com/Gateway

しかし、文字列を表示したくありませんassetType。重複した値も削除する必要があります。希望の出力は次のとおりです。

cloudfunctions.googleapis.com/CloudFunction
bigquery.googleapis.com/Dataset
artifactregistry.googleapis.com/Repository
composer.googleapis.com/Environment
apigateway.googleapis.com/Gateway

ベストアンサー1

あなたのもの(使用しているためgrepGNUであるように見え、すべて非標準GNU拡張です)がPCREサポートで構築されている場合は、次のことができます。grep-r-I-h

grep -rIihPo 'assetType:\s*\K.*' rules/resources/

正規表現に一致するテキストを出力するために使用した場所では、一致で非表示にする-o内容を指定するPerlに似た正規表現にo切り替えます。-P\KK

重複した項目を整列して削除するパイプですsort -u

おすすめ記事