【问题标题】:How to search the content of a file using sed如何使用 sed 搜索文件的内容
【发布时间】:2014-02-18 19:51:11
【问题描述】:

我尝试了几个命令并阅读了一些教程,但没有任何帮助。我正在使用 gnu sed 4.2.2。 我只想在文件中搜索:

func1(int a)
{

我不知道这是否是换行符后跟“{”的事实,但它不起作用。 一个正确的命令会有所帮助,并有更多的解释。谢谢!

注意:在“int a)”之后我输入了 enter,但我认为 stackoverflow 没有换行。我不想混淆,我只想搜索 func1(int a)'newline'{。

【问题讨论】:

标签: shell command-line sed gnu


【解决方案1】:

sed -n '/func1/{N;/func1(int a)\n{/p}'

解释:

sed -n '/func1/{                         # look for a line with "func1"
                N                        # append the next line
                /func1(int a)\n{/p       # try to match the whole pattern "func1(int a)\n{"
                                         # and print if matched
               }'                        

使用 grep 将是例如:

grep -Pzo 'func1\(int a\)\n{'

但请注意,感谢-zgrep 的输入将是一个包含换行符的大“行”(除非输入包含空字符)。在这种情况下必须对括号进行转义,因为它是一个 Perl 正则表达式 (-P)。 -o 让 grep 只打印匹配的模式。

【讨论】:

  • +1,并使其普遍匹配: sed -n '/func1/{N;/func1([^)]*)\s*\n\s*{/p}' 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多