regex - 根据预定义的标题使用 sed 选择文件部分

标签 regex linux sed text-processing

此表达式 sed -n '/statistics:/,/^ [^ ]/p' 选择以下部分

  Channel statistics:
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 114.237 (0.447987)
      standard deviation: 115.1 (0.451372)
      kurtosis: -1.92845
      skewness: 0.0962143
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 113.318 (0.444384)
      standard deviation: 113.041 (0.443298)
      kurtosis: -1.94057
      skewness: 0.0648024
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 111.01 (0.435332)
      standard deviation: 110.498 (0.433324)
      kurtosis: -1.92769
      skewness: 0.0747213
  Image statistics:

来自以下文件:

Image: /tmp/magick-XXpWFUXl
  Base filename: -
  Format: MIFF (Magick Image File Format)
  Class: DirectClass
  Geometry: 480x360+0+0
  Resolution: 72x72
  Print size: 6.66667x5
  Units: Undefined
  Type: TrueColor
  Base type: TrueColor
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 114.237 (0.447987)
      standard deviation: 115.1 (0.451372)
      kurtosis: -1.92845
      skewness: 0.0962143
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 113.318 (0.444384)
      standard deviation: 113.041 (0.443298)
      kurtosis: -1.94057
      skewness: 0.0648024
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 111.01 (0.435332)
      standard deviation: 110.498 (0.433324)
      kurtosis: -1.92769
      skewness: 0.0747213
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 84.6411 (0.331926)
      standard deviation: 109.309 (0.428662)
      kurtosis: -1.6052
      skewness: 0.582669
  Rendering intent: Undefined
  Interlace: None
  Background color: white
  Border color: rgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 480x360+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2011-12-07T12:33:31+02:00
    date:modify: 2011-12-07T12:33:31+02:00
    signature: f2adc51db916151ddcc5b206a8921eec0234efa1eeb7484c0046506b749bc392
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 179KB
  Number pixels: 173KB
  Pixels per second: 0b
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.6.0-4 2011-06-15 Q16 http://www.imagemagick.org
  • 它如何以及为何起作用?
  • 什么是 sed 格式?
  • 为什么选择 Channel statistics: 部分而不选择 Image statistics:

表达式来源取自下页http://www.imagemagick.org/Usage/compare/

最佳答案

Your expression: sed -n '/statistics:/,/^ [^ ]/ p'

它如何以及为何起作用?

sed 的自然形式遵循语法 sed 's/substitution/replacement/[g]' 其中 ssubstitution 和可选的 g 在末尾用于 global replacement(如果在一行中多次找到替换文本。

但是 sed 可以做的更多。它具有将操作限制在某些行的能力。您可以通过 -

 1. Specifying a line by its number. 
 2. Specifying a range of lines by number.
 3. All lines containing a pattern.
 4. All lines from the beginning of a file to a regular expression
 5. All lines from a regular expression to the end of the file.
 6. All lines between two regular expressions.

什么是sed格式?

您的 sed 格式 采用last 形式。它从包含 statistics: 的行开始执行它的魔法,直到从行首开始正好包含两个空格的行,即 __[^_] 其中 _ 是空格。即

sed -n '/statistics:/,/^ [^ ]/ p'
   |   ||           | |      | |
    ---  -----------   ------  V
     |        |           |    Since we suppressed
 Suppress This is     This is  the output, we need
  output   your         your   to invoke print
           start        end
           range       range

为什么选择 Channel statistics: 部分而不选择 Image statistics:

Image Statistics: 之后的原始文本行中缩进并且从行首开始有超过 2 个空格,因此它们不会显示。如果你想包括 Image Statistics: 你可以修改你的 Address End Range 像这样 -

sed -n '/statistics:/,/^  Ren.*/p'

为什么使用 -n 和 p?:​​sed 以其自然形式打印所有内容。每一行都放在 pattern space 中,所有操作都在其上执行,然后用新行打印该行。此处的操作是 p,这意味着整个文本将被打印,并且匹配 sed 的 范围的行将被打印两次。为了防止这种情况,我们调用了 -n-n 选项不会打印任何内容,除非找到对 print 的明确请求。

关于regex - 根据预定义的标题使用 sed 选择文件部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8413985/

相关文章:

regex - 了解正则表达式有多重要?

java - 正则表达式匹配数字和运算符之间的空格,但数字之间没有空格

linux - Debian 8 -> Debian 9 中的 Vi 变化

c++ - 无法将 Google Test NuGet 包添加到 Visual Studio Linux C++ 项目

bash - 如何删除分隔文件中标签之间的空格?

c++ - 使用 sed 命令换行字符串

html - Angular2/4 电话验证

linux - 如何在 gdb 中运行时修改二进制文件

python - 使用字段数将文件拆分为更小的文件

regex - Elasticsearch - 搜索跨越 2 个单词的子字符串