不以"//"开头的行的Grep [英] Grep for lines not beginning with "//"

查看:66
本文介绍了不以"//"开头的行的Grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试但未为grep编写不以"//"开头的行的正则表达式(即C ++样式的注释).我知道"grep -v"选项,但是我正在尝试学习如何仅使用正则表达式来实现这一点.我已经搜索并找到了各种答案,这些答案针对不以字符开头的行进行grepping处理,甚至还涉及如何针对不以字符串开头的行进行grep处理,但是我无法将这些答案适应我的要求的情况,我不明白我的错误是什么.

I'm trying but failing to write a regex to grep for lines that do not begin with "//" (i.e. C++-style comments). I'm aware of the "grep -v" option, but I am trying to learn how to pull this off with regex alone. I've searched and found various answers on grepping for lines that don't begin with a character, and even one on how to grep for lines that don't begin with a string, but I'm unable to adapt those answers to my case, and I don't understand what my error is.

> cat bar.txt
hello
//world
> cat bar.txt | grep "(?!\/\/)"
-bash: !\/\/: event not found

我不确定此找不到事件"的含义.我找到的答案之一是使用paren-question-mark-exclamation-string-paren,我已经在这里完成了,但仍然失败.

I'm not sure what this "event not found" is about. One of the answers I found used paren-question mark-exclamation-string-paren, which I've done here, and which still fails.

> cat bar.txt | grep "^[^\/\/].+"
(no output)

我发现的另一个答案是在方括号内使用插入符号,并解释说此语法的意思是搜索方括号中(除插入符号之外)不存在的内容.我认为.+"的意思是一个或多个一切",但我不确定这是否正确以及它是否正确,这与.*"有什么区别

Another answer I found used a caret within square brackets and explained that this syntax meant "search for the absence of what's in the square brackets (other than the caret). I think the ".+" means "one or more of anything", but I'm not sure if that's correct and if it is correct, what distinguishes it from ".*"

简而言之:如何构造正则表达式传递给grep来搜索不以"//"开头的行?

In a nutshell: how can I construct a regex to pass to grep to search for lines that do not begin with "//" ?

更具体地说,我正在尝试搜索以"//"开头的具有"#include"的行.

To be even more specific, I'm trying to search for lines that have "#include" that are not preceeded by "//".

谢谢.

推荐答案

第一行告诉您问题出在 bash (您的shell)上.Bash找到,并尝试将以 \////开头的最后一个输入的内容注入到命令中.为了避免这种情况,您需要转义或使用单引号.对于的示例,请尝试!cat ,它将执行您输入的 cat 开头的最后一条命令.

The first line tells you that the problem is from bash (your shell). Bash finds the ! and attempts to inject into your command the last you entered that begins with \/\/. To avoid this you need to escape the ! or use single quotes. For an example of !, try !cat, it will execute the last command beginning with cat that you entered.

您不需要转义/,它在正则表达式中没有特殊含义.您也不需要编写复杂的正则表达式来反转匹配.相反,只需为grep提供 -v 参数.大多数情况下,简单会更好.而且您也不需要将文件保存为grep.只需为grep提供文件名即可.例如.

You don't need to escape /, it has no special meaning in regular expressions. You also don't need to write a complicated regular expression to invert a match. Rather, just supply the -v argument to grep. Most of the time simple is better. And you also don't need to cat the file to grep. Just give grep the file name. eg.

grep -v "^//" bar.txt | grep "#include"

如果您真的想使用正则表达式,那么可以使用一个简单的表达式(匹配字符串 ^ 的开头,任意数量的空格 [[:space:]] **,正好两个反斜杠/{2} ,任意数量的任意字符.* ,后跟 #include ):

If you're really hungup on using regular expressions then a simple one would look like (match start of string ^, any number of white space [[:space:]]*, exactly two backslashes /{2}, any number of any characters .*, followed by #include):

grep -E "^[[:space:]]*/{2}.*#include" bar.txt

这篇关于不以"//"开头的行的Grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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