简体   繁体   中英

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. For example, if a line in a java file contains "log.info", I'd like to stick it in a file. 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. 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. 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? I imagine there must be something that can be done using sed or 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 -pe 's/:/\t/ while ++$x % 3'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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