Reputation: 1504
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.
Upvotes: 0
Views: 213
Reputation: 242038
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'
Upvotes: 1