【发布时间】:2017-10-06 16:53:17
【问题描述】:
我想在/* */ 或其他任何地方跳过 cmets 中的模式。
例如 f:
My name is
/*
alex
nice man
*/
alex is a nice man
命令:
git grep "alex" f
仅打印:
alex is a nice man
我更喜欢与 git grep 一起使用。
【问题讨论】:
-
我不明白你的回答@Cyrus
我想在/* */ 或其他任何地方跳过 cmets 中的模式。
例如 f:
My name is
/*
alex
nice man
*/
alex is a nice man
命令:
git grep "alex" f
仅打印:
alex is a nice man
我更喜欢与 git grep 一起使用。
【问题讨论】:
grep 代表g/re/p,即全局搜索正则表达式并打印匹配的字符串。您要做的远远超出了那个简单的语句,所以您应该使用 awk:
$ awk 'index($0,"/*"){f=1} index($0,"*/"){f=0} !f && /alex/{print}' file
alex is a nice man
虽然以上内容适用于您展示的简单示例,但对于其他情况,例如一行上的多个 cmets 或目标行的开头/结尾处的 cmets 或其他构造中的注释定界符(例如字符串内),它将失败。
如果您有这些情况(例如,在 C 或类似程序中),那么您不应该尝试使用文本处理工具来执行此操作,您需要一个语言解析器,例如见https://stackoverflow.com/a/35708616/1745001
【讨论】: