什么是&QUOT意义; 0 / XXX"在sed? [英] What is the meaning of "0,/xxx" in sed?

查看:165
本文介绍了什么是&QUOT意义; 0 / XXX"在sed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在脚本中使用的sed命令如下:

A sed command used in a script as following:

sed -i "0,/^ENABLE_DEBUG.*/s/^ENABLE_DEBUG.*/ENABLE_DEBUG = YES/" MakeConfig

我知道

s/^ENABLE_DEBUG.*/ENABLE_DEBUG = YES/

是替代线preFIX

is to substitutes line prefix

ENABLE_DEBUG ENABLE_DEBUG = YES

但没有关于意义的想法

0,/^ENABLE_DEBUG.*/

任何人都可以帮我吗?

Anyone can help me?

推荐答案

0,/ ^ ENABLE_DEBUG。* / 指取代只会从一开始线路发生, 0 ,到与其匹配的第一行 / ^ ENABLE_DEBUG。* / 。没有替代将在后续的行作出,即使它们匹配 / ^ ENABLE_DEBUG。* /

0,/^ENABLE_DEBUG.*/ means that the substitution will only occur on lines from the beginning, 0, to the first line that matches /^ENABLE_DEBUG.*/. No substitution will be made on subsequent lines even if they match /^ENABLE_DEBUG.*/

这将只行2至5代:

sed '2,5 s/old/new/'

这将取代从第2行,第一行就包括在的东西

This will substitute from line 2 to the first line after it which includes something:

sed '2,/something/ s/old/new/'

这将包含的东西到文件末尾的第一行替换:

This will substitute from the first line that contains something to the end of the file:

sed '/something/,$ s/old/new/'

POSIX与GNU范围:线路为0

的意义

考虑这个测试文件:

POSIX vs. GNU ranges: the meaning of line "0"

Consider this test file:

$ cat test.txt
one
two
one
three

现在,让我们应用SED在范围 1,/一个/

Now, let's apply sed over the range 1,/one/:

$ sed '1,/one/ s/one/Hello/' test.txt
Hello
two
Hello
three

范围与1号线开始,以之后的第一行的结尾匹配行1 有一个。因此,两个换人上面的。

The range starts with line 1 and ends with the first line after line 1 that matches one. Thus two substitutions are made above.

假设我们只想在第一个有一个替换。符合POSIX sed的,这不能与范围进行。由于NeronLeVelu指出,G​​NU SED提供了这种情况下的扩展:它允许我们指定范围为 0,/一个/ 。该范围内第一次出现的结束有一个中的文件:

Suppose that we only wanted the first one replaced. With POSIX sed, this cannot be done with ranges. As NeronLeVelu points out, GNU sed offers an extension for this case: it allows us to specify the range as 0,/one/. This range ends with the first occurrence of one in the file:

$ sed '0,/one/ s/one/Hello/' test.txt
Hello
two
one
three

因此​​,范围 0,/ ^ ENABLE_DEBUG / 有与 ENABLE_DEBUG 开头的第一线,即使结束该行是第一行。这需要GNU sed的。

Thus, the range 0,/^ENABLE_DEBUG/ ends with the first line that begins with ENABLE_DEBUG even if that line is the first line. This requires GNU sed.

这篇关于什么是&QUOT意义; 0 / XXX"在sed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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