简体   繁体   English

尝试使用 sed 删除范围内的某些行

[英]Trying to Delete Certain Lines in a Range Using sed

In a large file, I need to edit text and remove comments inside a particular range.在一个大文件中,我需要编辑文本并删除特定范围内的注释。 For this simplified example, let's assume the range begins with _start_ and finishes at _end_ .对于这个简化的示例,我们假设范围以_start_开始并在_end_结束。

I'm able to edit the text with no problem using a command like:我可以使用如下命令毫无问题地编辑文本:

sed -i -r "/_start_/,/_end_/ s/SearchText/ReplaceText/" FileName

Please note the following (and let me know, of course, if any of my statements are inaccurate or misguided):请注意以下几点(当然,如果我的任何陈述不准确或有误,请告诉我):

  • I used -i so that it would edit "FileName" in place, rather than write to a different file.我使用-i以便它可以就地编辑“文件名”,而不是写入不同的文件。
  • I used -r so that it would recognize extended regular expressions (which are not shown in the simplified example above, but which seem to be working correctly).我使用-r以便它可以识别扩展的正则表达式(上面的简化示例中没有显示,但似乎可以正常工作)。
  • I used double-quotes so that it would correctly handle variables (also not shown in the simplified example above, but also working as expected).我使用了双引号,以便它可以正确处理变量(上面的简化示例中也没有显示,但也按预期工作)。

That command above is doing exactly what I expect it to do.上面的命令完全符合我的预期。 So I moved on to the second step of my process: a very similar command to remove comment lines within this range:所以我继续我的过程的第二步:一个非常相似的命令来删除这个范围内的注释行:

sed -i -r "/_start_/,/_end_/ /^#/ d" FileName

This, however, is having no effect: The lines that begin with # are not removed.但是,这没有任何效果:以#开头的行不会被删除。 Indeed, when I execute this command alone, absolutely nothing in my file is changed or deleted -- neither inside the range nor elsewhere.事实上,当我单独执行这个命令时,我的文件中绝对没有任何内容被更改或删除——无论是在范围内还是在其他地方。

In my searches on this site and elsewhere, I've found a lot of instructions on deleting lines using sed (instructions that I think I'm following correctly) -- but nothing about a failure such as I'm experiencing.在本网站和其他地方的搜索中,我发现了很多关于使用 sed 删除行的说明(我认为我正确遵循的说明)——但没有关于我遇到的失败的信息。

Can anyone advise me what I'm doing wrong here?谁能告诉我我在这里做错了什么?

I'm very new to the UNIX/Linux environment, so I'm definitely open to alternate suggestions as to how to handle the issue.我是 UNIX/Linux 环境的新手,所以我绝对乐于接受有关如何处理该问题的其他建议。 But just to satisfy my frustration, I'd love to know what's wrong with my sed command above.但为了满足我的挫败感,我很想知道上面的 sed 命令有什么问题。

The best source of information is often the man page. 最佳信息来源通常是手册页。 You can reach it with the command man sed . 您可以使用命令man sed访问它。

d takes an address range according to the man page. d根据手册页获取地址范围。 An address can be a number, a /regexp/, or a number of other things. 地址可以是数字,/ regexp /或许多其他内容。 An address range is either one address or two addresses, separated by comma. 地址范围是一个地址或两个地址,以逗号分隔。 You have been trying to use an address range and then an address. 您一直在尝试使用地址范围,然后使用地址。

As 1_CR pointed out, you can work around by using a block instead: 正如1_CR指出的那样,您可以使用块来解决:

sed -i -r "/_start_/,/_end_/ {/^#/ d}" FileName

A block accepts an address range, and every command accepts an address range again, so you can combine the regexps. 块接受地址范围,并且每个命令再次接受地址范围,因此您可以组合正则表达式。

You need to change 你需要改变

sed -i -r "/_start_/,/_end_/ /^#/ d" FileName

to

sed -i -r "/_start_/,/_end_/{/^#/d}" FileName

In terms of doing exactly what your question asks, you can also do the same thing with a range of line numbers.就完全按照您的问题进行操作而言,您也可以使用一系列行号来做同样的事情。 It doesn't use regular expressions, but you might find doing this is easier if looking at the line numbers is convenient for you:它不使用正则表达式,但如果查看行号对您来说方便的话,您可能会发现这样做会更容易:

sed -i '<start-of-range>,<end-of-range>d' FileName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM