使用 sed 基于预定义标头选择文件的部分

发布于 2024-12-19 23:54:08 字数 2740 浏览 1 评论 0原文

选择以下部分

  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:

此表达式 sed -n '/statistics:/,/^ [^ ]/ p'从以下文件中

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 stats: 部分而不是 Image stats:

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

This expression sed -n '/statistics:/,/^ [^ ]/ p' selects the following section

  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:

From the following file:

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
  • How and why does it work?
  • What is the sed format ?
  • Why it selects the Channel statistics: section but not the Image statistics: ?

The source of the expression is taken from the following page http://www.imagemagick.org/Usage/compare/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-12-26 23:54:08

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

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

sed 的自然形式遵循语法 sed 's/substitution/replacement/[g]',其中 s 代表 substitution< /strong> 以及结尾处的可选 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.

What is the sed format ?

您的 sed format 采用最后一种形式 来实现这一点。 >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

。选择频道 :

stats: 部分,但不是 Image stats 行,因此它们不会显示。如果您想包含图像统计信息:,您可以修改您的地址结束范围,如下所示 -

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

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

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

How and why does it work?

sed in it's natural form follows the syntax sed 's/substitution/replacement/[g]' where s is for substitution and an optional g at the end for global replacement (if substitution text is found more than once in a line.

But sed can do much more. It has the ability to restrict the operation to certain lines. You can do that by -

 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.

What is the sed format ?

Your sed format takes the last form. It starts performing it's magic from the line that contains statistics: to the line that contains exactly two spaces from the beginning of the line i.e __[^_] where _ is the space. i.e

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

Why it selects the Channel statistics: section but not the Image statistics:?

In your original text lines after Image Statistics: are indented and have more than 2 spaces from the beginning of the line as a result they are not displayed. If you want to include Image Statistics: you can modify your Address End Range to be like this -

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

Why -n and p?:sed in it's natural form prints everything. Each line is put in the pattern space upon which all actions are performed on it and then the line is printed with a new line. The action over here is p which means entire text will be printed and the lines that matches the sed's range will be printed twice. To prevent this we invoke the -n. The -n option will not print anything unless an explicit request to print is found.

原来分手还会想你 2024-12-26 23:54:08

它如何以及为什么起作用?

见下文

sed 格式是什么?

format is sed address1, address2

>为什么它选择“通道统计:”部分而不是“图像统计:”?

首先,我想说您问题中的 sed 行与链接中的行不完全相同。它应该是 sed -n '/statistics:/,/^(two space)[^ ]/ 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

我认为这与您的文件非常相似。

sed 是做什么的?

1 找出address1的第一个匹配项,即/x/,因此找到x_1
并接受。

2 然后查找地址2,/^[^]/ 如果不匹配,则打印。这个正则表达式
意味着该行不以空格开头。

3 x_2 以 x 开头,而不是空格,因此匹配 address2,打印。

在x_2之后,没有更多的行匹配address1 /x/,所以x_2应该
是最后一行

是相同的,唯一的区别是您的情况下的地址2是以两个空格开头的行。

只是我的 2 美分。希望有帮助。

How and why does it work?

see below

What is the sed format ?

format is sed address1, address2

> Why it selects the Channel statistics: section but not the Image statistics: ?

first, I would say that the sed line in your question is not exactly the same as the line in your link. it should be sed -n '/statistics:/,/^(two spaces)[^ ]/ p'

see this example:

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

I think this would be quite similar as your file.

what sed does?

1 find out the 1st match of address1, which is /x/, so x_1 was found
and accepted.

2 then searching the address2, /^[^ ]/ if not match, print. this regex
means, the line not starts with space.

3 x_2 starts with x, not space, so match address2, print.

after x_2, there is no more line matching address1 /x/, so x_2 should
be the last line

your image file example is the same, only difference is address2 in your case is line starting with two spaces.

just my 2 cents. hope helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文