如何在Linux命令行中grep以双正斜杠开头的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6552966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grep lines that start with double forward slash in Linux command line?
提问by FinalForm
How do I grep lines in a file that start with double forward slash from the Linux command line? The double forward slashes may have spaces before it, but only spaces before it and no other types of characters.
如何从 Linux 命令行 grep 以双正斜杠开头的文件中的行?双正斜杠前面可能有空格,但前面只有空格,不能有其他类型的字符。
e.g.
例如
shell_prompt: ls
shell_prompt: grep .....? file_name
Sorry was being unobservant, I didn't notice something that existed before the // characters other than the space. Problem solved. Will award to the guy that answered first.
对不起,我没有注意,我没有注意到除空格之外的 // 字符之前存在的东西。问题解决了。将奖励给第一个回答的人。
采纳答案by Micha? Trybus
Use regular expressions:
使用正则表达式:
grep -E '^ *//'
回答by Matt D
Edit: read all the requirements:
编辑:阅读所有要求:
grep "^ *\/\/" file
You need two backslashes for escape characters in the shell because you are passing the backslash character to the grep command.
在 shell 中,您需要两个反斜杠作为转义字符,因为您将反斜杠字符传递给 grep 命令。
i.e. grep sees the regex string \/\/
即 grep 看到正则表达式字符串 \/\/
回答by Matt Sieker
grep "//" jquery.js
will find all lines that contain //
. Which if your looking for comments, what you might be interested in.
将找到所有包含//
. 如果您正在寻找评论,您可能会感兴趣。
grep "^//" jquery.js
will find just lines that start with //
.
只会找到以//
.开头的行。
It might me worth your while to start looking into Regular Expressions.
开始研究正则表达式可能值得你花时间。