2016-03-27 13 views
-3

私は何百もの単語を含むファイルを持っています。 ファイルの一部のセクションにアンダースコア(_)で始まり、カンマ(、)で区切られた単語があります。アンダースコアで始まる単語だけをコンマで区切って検索し、配列に保存したいと考えています。どうすればいいですか?シェルスクリプトを使用したファイルのパターンマッチング

しかし、それは単語ではなく行をリストします。

サンプルファイル(Appleのtbdファイル)。私はアンダースコアで始まる単語のリストを取得したい

archs:   [ armv7, armv7s, arm64 ] 
platform:  ios 
install-name: /System/Library/Frameworks/AVFoundation.framework/AVFoundation 
current-version: 2.0 
objc-constraint: retain_release 
exports: 
    - archs:   [ armv7, armv7s, arm64 ] 
    re-exports:  [ /System/Library/Frameworks/AVFoundation.framework/libAVFAudio.dylib ] 
    symbols:   [ _AVAssetAssociatedSubtitlesTrackReferencesKey, _AVAssetChapterListTrackReferencesKey, 
         _AVAssetChapterMetadataGroupsDidChangeNotification, 
         _AVAssetDownloadSessionAirPlayAuthorizationInfoKey, 
         _AVAssetDownloadSessionCachePrimingDownloadTokenKey, 
         _AVAssetDownloadSessionClientAuditTokenKey, _AVAssetDownloadSessionClientBundleIdentifierKey, 
         _AVAssetDownloadSessionCurrentLoadedTimeRangesKey, 
         _AVAssetDownloadSessionDeleteDownloadWhenAssetFinalizesKey, 
         _AVAssetDownloadSessionDidResolveMediaSelectionNotification, 
         _AVAssetDownloadSessionDownloadFailedNotification, 
         _AVAssetDownloadSessionDownloadSucceededNotification, 
         _AVAssetDownloadSessionFileSizeAvailableNotification, 
         _AVAssetDownloadSessionHTTPCookiesKey, _AVAssetDownloadSessionHTTPHeaderFieldsKey, 
         _AVAssetDownloadSessionLoadedTimeRangesChangedNotification, 
         _AVAssetDownloadSessionMaxSizeAllowedForCellularAccessKey, 
         _AVAssetDownloadSessionMediaSelectionKey, _AVAssetDownloadSessionMinimumRequiredMediaBitrateKey, 
         _AVAssetDownloadSessionNewlyLoadedTimeRangeKey, 
         _AVAssetDownloadSessionOptimizeAccessForLinearMoviePlaybackKey, 
         _AVAssetDownloadSessionPreferredAudibleMediaCharacteristicKey, 
         _AVAssetDownloadSessionPreferredLegibleMediaCharacteristicKey, 
         _AVAssetDownloadSessionPreferredVisualMediaCharacteristicKey, 
         _AVAssetDownloadSessionPriorityKey, _AVAssetDownloadSessionProtectedContentSupportStorageURLKey, 
         _AVAssetDownloadSessionPurchaseBundleKey, _AVAssetDownloadSessioniTunesStoreContentDSIDKey, 
         _AVAssetDownloadSessioniTunesStoreContentDownloadParametersKey, 
         _AVAssetDownloadSessioniTunesStoreContentIDKey, 
         _AVAssetDownloadSessioniTunesStoreContentInfoKey, 
         _AVAssetDownloadSessioniTunesStoreContentPurchasedMediaKindKey, 
         _AVAssetDownloadSessioniTunesStoreContentTypeKey, 
         _AVAssetDownloadSessioniTunesStoreContentUserAgentKey, 
         _AVAssetDownloadTaskMediaSelectionKey, _AVAssetDownloadTaskMinimumRequiredMediaBitrateKey, 
         _AVAssetDurationDidChangeNotification, _AVAssetExportPreset1280x720, 
         _AVAssetExportPreset1920x1080, _AVAssetExportPreset3840x2160, 
         _AVAssetExportPreset3GPRelease6MMS, _AVAssetExportPreset640x480, 
         _AVAssetExportPreset960x540, _AVAssetExportPresetAppleM4A, 
         _AVAssetExportPresetAudioOnlyMMS, _AVAssetExportPresetAuxSmall, 
+3

あなたはいくつかの行を示すことができ、どのように結果があなたの例のためにのように見えるのだろうか? –

+0

「_words」を含む*セクション*とファイルの他の*セクション*を区別する方法の詳細を教えてください。良いスタートは、各タイプのセクションを持つサンプルを追加することです。 –

答えて

2

あなたは試すことができます:

$ grep -o ' _[a-zA-Z]*' <filename> 

出力:

_AVAssetAssociatedSubtitlesTrackReferencesKey 
_AVAssetChapterListTrackReferencesKey 
_AVAssetChapterMetadataGroupsDidChangeNotification 
_AVAssetDownloadSessionAirPlayAuthorizationInfoKey 
_AVAssetDownloadSessionCachePrimingDownloadTokenKey 
_AVAssetDownloadSessionClientAuditTokenKey 
_AVAssetDownloadSessionClientBundleIdentifierKey 
_AVAssetDownloadSessionCurrentLoadedTimeRangesKey 
_AVAssetDownloadSessionDeleteDownloadWhenAssetFinalizesKey 
_AVAssetDownloadSessionDidResolveMediaSelectionNotification 
... 

ストア配列arrでの結果:

$ words=`grep -o ' _[a-zA-Z]*' <filename>` 
$ read -a arr <<<$words 

出力arrの要素:

$ for elem in ${arr[*]} 
> do 
> echo ${elem} 
> done 
_AVAssetAssociatedSubtitlesTrackReferencesKey 
_AVAssetChapterListTrackReferencesKey 
_AVAssetChapterMetadataGroupsDidChangeNotification 
_AVAssetDownloadSessionAirPlayAuthorizationInfoKey 
_AVAssetDownloadSessionCachePrimingDownloadTokenKey 
_AVAssetDownloadSessionClientAuditTokenKey 
_AVAssetDownloadSessionClientBundleIdentifierKey 
_AVAssetDownloadSessionCurrentLoadedTimeRangesKey 
_AVAssetDownloadSessionDeleteDownloadWhenAssetFinalizesKey 
_AVAssetDownloadSessionDidResolveMediaSelectionNotification 
... 
+0

素敵なコピー&ペースト...自分の投稿を削除できません。ロックされています。明日私は重複した答えを避けるためにそれを行うでしょう。 – SLePort

関連する問題