简体   繁体   English

忽略引号之间的字符grep

[英]Ignore characters in between quotes grep

I'm trying to write a simple script which will find all java files in a directory and display all the log messages which match some search string. 我正在尝试编写一个简单的脚本,该脚本将在目录中找到所有Java文件并显示与某些搜索字符串匹配的所有日志消息。 For example, if a line in a java file contains "log.info", I'd like to stick it in a file. 例如,如果java文件中的一行包含“ log.info”,我想将其粘贴到文件中。 The same goes for "error", "warn", and "debug". “错误”,“警告”和“调试”也是如此。 Right now this is what I have: 现在,这就是我所拥有的:

grep -rn --include \*.java "\b\.error\b" * >> log_strings.csv

Then I manually replace ".error" with one of the other strings. 然后,我用其他字符串之一手动替换“ .error”。 However, when I try to later separate the output, grep adds colon ":" chars between the fields, and some of the log messages also contain ":" chars, so my output gets messy. 但是,当我稍后尝试分离输出时,grep在字段之间添加冒号“:”字符,并且某些日志消息也包含“:”字符,因此我的输出变得混乱。 I end up with: 我最终得到:

filename:line_num:log.debug("some message:"+whatever());

roughly translated to: 大致翻译为:

filename **TAB** line_num **TAB** log.debug("some message **TAB** "+whatever());

Is there a way to replace the ":" character that grep produces but ignore the one with in the string so that my java strings stay organized? 有没有办法替换grep生成的“:”字符,但忽略该字符串中的那个,这样我的Java字符串就可以井井有条了? I imagine there must be something that can be done using sed or awk. 我想必须使用sed或awk可以完成某些工作。

If you want to replace the first two colons by tabs, just replace one colon twice: 如果要用制表符替换前两个冒号,只需将一个冒号替换两次:

sed 's/:/\t/;s/:/\t/;'

In Perl, you can avoid repeating the substitution by running it in a loop: 在Perl中,可以通过在循环中运行替换来避免重复替换:

perl -pe 's/:/\t/ while ++$x % 3'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM