Regex 基于预定义头使用sed选择文件节

Regex 基于预定义头使用sed选择文件节,regex,linux,sed,text-processing,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: mi

此表达式
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格式是什么
  • 为什么选择
    频道统计信息:
    部分,而不选择
    图像统计信息:
表达式的源代码取自下一页

它是如何工作的以及为什么工作的?

见下文

什么是sed格式?

>为什么选择频道统计:部分而不是图像统计:?

首先,我想说的是,你问题中的sed行与你链接中的sed行并不完全相同。它应该是
sed-n'/statistics:/,/^(两个空格)[^]/p'

请参见此示例:

kent$  cat file1
x_1
 1
 2
 3
 o
x_2
 4
 5
 6

kent$  sed -n '/x/,/^[^ ]/p' file1
x_1
 1
 2
 3
 o
x_2
我想这和你的档案很相似

塞德是干什么的

1找到地址1的第一个匹配项,即/x/,因此找到了x_1 接受了

2然后搜索地址2,如果不匹配,则打印。这个正则表达式 意思是,这条线不是以空格开头的

3 x_2以x开头,不是空格,所以匹配地址2,打印

在x_2之后,没有更多与address1/x/匹配的行,因此x_2应该 是最后一行吗

您的图像文件示例是相同的,唯一的区别是address2在您的示例中是以两个空格开头的行

只要我的2美分。希望对你有帮助

它是如何工作的以及为什么工作的?

见下文

什么是sed格式?

>为什么选择频道统计:部分而不是图像统计:?

首先,我想说的是,你问题中的sed行与你链接中的sed行并不完全相同。它应该是
sed-n'/statistics:/,/^(两个空格)[^]/p'

请参见此示例:

kent$  cat file1
x_1
 1
 2
 3
 o
x_2
 4
 5
 6

kent$  sed -n '/x/,/^[^ ]/p' file1
x_1
 1
 2
 3
 o
x_2
我想这和你的档案很相似

塞德是干什么的

1找到地址1的第一个匹配项,即/x/,因此找到了x_1 接受了

2然后搜索地址2,如果不匹配,则打印。这个正则表达式 意思是,这条线不是以空格开头的

3 x_2以x开头,不是空格,所以匹配地址2,打印

在x_2之后,没有更多与address1/x/匹配的行,因此x_2应该 是最后一行吗

您的图像文件示例是相同的,唯一的区别是address2在您的示例中是以两个空格开头的行

只要我的2美分。希望对你有帮助

您的表达式:
sed-n'/statistics:/,/^[^]/p'

它是如何工作的,为什么工作?
sed
在其自然形式中遵循语法
sed's/substitution/replacement/[g]
,其中
s
表示替换,并且在全局替换的结尾处有一个可选的
g
(如果在一行中发现多个替换文本)

但是
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格式
采用最后一种形式。它从包含
统计信息的行开始执行它的魔力:
到从行首开始正好包含两个空格的行,即
\uuu[^\ u]
,其中
\uu
是空格

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
为什么选择
频道统计信息:
部分,而不选择
图像统计信息:
? 在原始文本中,
图像统计信息之后的行:
缩进,因此与行首的距离超过2个空格。如果要包含
图像统计信息:
,可以将
地址结束范围修改为如下所示-

sed -n '/statistics:/,/^  Ren.*/p'
为什么-n和p?:
sed
以其自然形式打印所有内容。每一行都放在
模式空间中,在该空间上执行所有操作,然后用新行打印该行。这里的操作是
p
,这意味着将打印整个文本和匹配的行
sed的
范围将被打印两次。为了防止出现这种情况,我们调用了
-n
-n
选项将不会打印任何内容,除非找到打印
的明确请求

您的表达式:
sed-n'/statistics:/,/^[^]/p'

它是如何工作的,为什么工作?
sed
在其自然形式中遵循语法
sed's/substitution/replacement/[g]
,其中
s
表示替换,并且在全局替换的结尾处有一个可选的
g
(如果在一行中发现多个替换文本)

但是
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格式
采用最后一种形式。它从包含
统计信息的行开始执行它的魔力:
到从行首开始正好包含两个空格的行,即
\uuu[^\ u]
,其中
\uu
是空格

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
为什么选择
频道统计信息:
部分,而不选择
图像统计信息:
? 在原始文本中,
图像统计信息之后的行:
缩进,因此与行首的距离超过2个空格。如果要包含
图像统计信息:
,可以将
地址结束范围修改为如下所示-

sed -n '/statistics:/,/^  Ren.*/p'
为什么-n和p?:
sed
以其自然形式打印所有内容。每一行都放在
模式空间中,在该空间上执行所有操作,然后用新的li打印该行