简体   繁体   English

使用sed,如何删除与任何一组模式匹配的所有初始行?

[英]Using sed, how do I delete all initial lines that match any of a set of patterns?

I'm looking to use sed to delete all of the initial lines in a file that match a set of patterns. 我正在寻找使用sed删除文件中与一组模式匹配的所有初始行。 But, once none of the patterns are matched, the rest of the file should be passed through unmodified. 但是,一旦没有任何一种模式匹配,文件的其余部分应通过未修改的方式传递。

For example, if the patterns are: 例如,如果模式是:

^\s*#
^\s*$

Then for the file: 然后对于文件:

#!/bin/bash
# Some words

# The line above has spaces or tabs in it
    #
    # The above line has spaces before and after the #

main()
{ ... }

# Another comment

the sed script would generate: sed脚本将生成:

main()
{ ... }

# Another comment

That is, in this case the set of patterns removes any initial Bash comments and, blank lines, and lines that just have whitespace in them. 也就是说,在这种情况下,模式集将删除所有初始的 Bash注释以及空白行和仅包含空格的行。

While it might be interesting to hear how this might be done with other tools (such as awk ), I am mainly interested in solutions involving sed . 听到使用其他工具(例如awk )如何完成此工作可能会很有趣,但我主要对涉及sed解决方案感兴趣。

I'm looking for a specific solution to the example above as well as any guidance as to how to generalize this for a set of arbitrary patterns. 我正在寻找上述示例的特定解决方案,以及有关如何将其概括为一组任意模式的任何指导。

My environment is: 我的环境是:

  • CentOS 7.3 (3.10.0-514.6.1.el7.x86_64) CentOS 7.3(3.10.0-514.6.1.el7.x86_64)
  • bash-4.2.46-21.el7_3.x86_64 bash-4.2.46-21.el7_3.x86_64
  • sed-4.2.2-5.el7.x86_64 sed-4.2.2-5.el7.x86_64

Delete lines that do match, but once you get past all the patterns, just print the rest using the loop :done;n;bdone 删除确实匹配的行,但是一旦您:done;n;bdone所有模式,只需使用循环:done;n;bdone打印其余的:done;n;bdone

Here's the example: 这是示例:

test.c: test.c:

#!/bin/bash
# Some words

# The line above has spaces or tabs in it
    #
    # The above line has spaces before and after the #

DELETEME

main()
DELETEME - should stay
{ ... }



$ sed '/^\s*#/d; /DELETEME/d; /^\s*$/d; :done;n;bdone' test.c 
main()
DELETEME - should stay
{ ... }

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

相关问题 sed匹配3行模式并删除2 - sed match 3 lines patterns and delete 2 如何使用SED在文件的两个连续行中搜索两个不同的模式,并在模式匹配后打印下4行? - How can I search for two different patterns in two consecutive lines in a file using SED and print next 4 lines after pattern match? 如何删除与sed模式第二次匹配之前的所有行? - How to delete all lines before the second match of a pattern with sed? 如何使用sed删除多行两个模式之间的内容? - How do i remove the content between two patterns on multiple lines using sed? 使用sed,如何删除一系列行中的所有字符串实例? - Using sed, how can I delete all instances of a string within a range of lines? 如何删除带有 sed 的文件的部分行? - How do I delete part of the lines of a file with sed? 如何使用SED或AWK在文件中打印两个图案之间的线? - how to print Lines Between Two Patterns in file using SED or AWK? 比较2个文件并使用awk / sed / bash删除符合条件的行 - Compare 2 files and delete the lines that match criteria using awk / sed /bash 如何选择标记之间的多行(*),但最后一行除外(使用sed)? 而我该如何选择其余的呢? - How do i select multiple lines between markers (*) excluding the last one (using sed)? And how do I select all the rest? 如何使用sed命令在bash脚本中追加行? - How do I append lines in a bash script using sed command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM