我正在尝试执行一个grep命令,查找文件中的所有行,其中第一个单词以"as"开头,而第一个单词也以"ng"结尾
我将如何使用grep进行此操作?
这应该就是这样做的:
$ grep '^as\w*ng\b' file
Regexplanation:
^ # Matches start of the line as # Matches literal string as \w # Matches characters in word class * # Quantifies \w to match either zero or more ng # Matches literal string ng \b # Matches word boundary
可能已经错过了奇怪的角落案例.
如果您只想打印匹配的单词而不是整行,请使用以下-o
选项:
$ grep -o '^as\w*ng\b' file
阅读man grep
有关可用选项的所有信息.