【问题标题】:Pattern matching in a file using Shell script使用 Shell 脚本在文件中进行模式匹配
【发布时间】:2016-03-27 12:02:37
【问题描述】:

我有一个包含数百个单词的文件。 文件的某些部分包含以下划线 (_) 开头并用逗号 (,) 分隔的单词。我只想找到以逗号分隔的下划线开头的单词并将其保存在某个数组中。我该怎么做?

I tried cat <filename> grep _*

但它列出了行而不是单词。

示例文件(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,

【问题讨论】:

  • 您能否展示一些行以及您的示例的结果如何?
  • 您能否详细说明包含“_words”的部分以及如何区分文件的其他部分?一个好的开始是添加一个包含每种类型部分的示例。

标签: regex bash shell awk sed


【解决方案1】:

你可以试试:

$ 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
...

【讨论】:

  • 不错的复制和粘贴...无法删除我的帖子,它已被锁定。我明天会这样做以避免重复答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 2022-12-04
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多