Sed 地址范围中的空正则表达式有什么作用? [英] What Does an Empty Regular Expression in a Sed Address Range Do?

查看:39
本文介绍了Sed 地址范围中的空正则表达式有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的命令 sed -E '/# Section [134]/,//s/foo/bar/' <input_file> 完成以下操作

The following command sed -E '/# Section [134]/, // s/foo/bar/' <input_file> accomplishes the following

输入

# Section 1

- foo
- Unimportant Item

# Section 2

- foo
- Unimportant Item

# Section 3

- foo
- Unimportant Item

# Section 4

- foo
- Unimportant Item

# Section 5

- foo
- Unimportant Item

输出

# Section 1

- bar
- Unimportant Item

# Section 2

- foo
- Unimportant Item

# Section 3

- bar
- Unimportant Item

# Section 4

- bar
- Unimportant Item

# Section 5

- foo
- Unimportant Item

我不确定这个命令是如何工作的,特别是地址范围内的空正则表达式.到目前为止我所了解的是 Sed 将首先查找与以下正则表达式 /# Section [134]/ 匹配的文档部分,如果匹配,它将开始替换,寻找 /# Section [134]/ 的匹配项code>foo 并将它们替换为 bar.地址范围的第二部分,据我所知,是停止点,但在这种情况下它是空的.我读到 here 一个空的正则表达式重复最后一个正则表达式匹配",但我不完全知道这意味着什么,或者它如何影响这个特定的 Sed 命令.地址范围如何知道停止点在每个部分之后?// 重复的是什么正则表达式?

I am unsure of how this command works, specifically the empty regular expression in the address range. What I understand so far is Sed will first look for a portion of the document that matches the following regex /# Section [134]/, if it matches, it will begin the substitution looking for matches for foo and replacing them with bar. The second portion of the address range, as far as I am aware, is the stopping point, but in this case it is empty. I read here that an empty regular expression "repeats the last regular expression match", but I don't exactly know what this means, or how it affects this specific Sed command. How does the address range know that the stopping point is after each section? What regex is // repeating?

推荐答案

让我把输入文件简化为:

Let me simplify the input file as:

Line1
Line2
Line3
Line4
Line5

和测试脚本为:

sed -n "/[134]/,//p"

将打印与您的测试结果相对应的所有行.如前所述,空正则表达式重复之前的正则表达式,然后是 sed 命令以上相当于:

which will print the all lines, corresponding to your test results. As noted, the empty regex repeats the previous regex, then the sed command above is equivalent to:

sed -n "/[134]/,/[134]/p"

顺便说一句,sed 的地址范围操作符的工作原理如下:

BTW the address range operator of sed works as follows:

  • 如果 left 地址匹配,则返回 true 评估right 地址在同一行上(与 awk 的范围运算符不同,它评估右条件立即在同一行).
  • If the left address matches, returns true without evaluating right address on the same line (unlike the range operator of awk which evaluates the right condition immediately on the same line).

让我们一行一行地看看操作符是如何工作的.

Let's see how the operator works line by line.

  • 在第一行Line1,左边的起始地址匹配并且返回 true.
  • 在第二行,右停止地址被评估而没有匹配则运算符保持 true.
  • 在第三行,右边的停止地址匹配然后它将状态更改为 false(在打印该行之后).
  • 在第四行,左边的起始地址匹配并且再次返回 true.
  • 第五行,右边的停止地址不匹配,保持true.
  • On the first line Line1, the left start address matches and returns true.
  • On the second line, the right stop address is evaluated without match then the operator keeps true.
  • On the third line, the right stop address matches then it changes the status to false (after printing the line).
  • On the fourh line, the left start address matches and returns true again.
  • On the fifth line, the right stop address does not match and keeps true.

如果您将正则表达式更改为 /[135]/,您将看到不同的结果.(第 1、2、3、5 行将跳过第 4 行打印.)

If you change the regex to /[135]/, you will see a different result. (Line1, 2, 3, 5 will be printed skipping Line4.)

这篇关于Sed 地址范围中的空正则表达式有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆