I am trying to write a simple wrapper for grep in order to put its output in a more readable format. This includes putting the matched string (which occurs after the second colon) on a new line, and trimming any leading whitespace/tabs from the matched string.
So instead of doing the following:
$ grep -rnIH --color=always "grape" .
./apple.config:1: Did you know that grapes are tasty?
I would like to be able to get this:
$ grep -rnIH --color=always "grape" . | other-command
./apple.config:1:
Did you know that grapes are tasty?
I have tried many different methods to try to do this, including using sed, awk itself, substitution, perl etc. One important thing to keep in mind is that I want to trim leading space from $3, but that $3 may not actually contain the entire matched string (for example, if the matched string contains a url with ":" characters).
So far I have gotten to the point that I have the following.
$ grep -rnIH --color=always "grape" . | \
awk -F ":" '{gsub(/^[ \t]+/, "", $3); out=""; for(i=4;i<=NF;i++){out=out$i}; print $1":"$2"\n"$3out}'
./apple.config:1:
Did you know that grapes are tasty?
The gsub is intended to trim whitespace/tabs from the start of whatever occurs right after the second colon. Then the for loop is intended to build a variable made up of anything else in the matched string that may have gotten split by the field separator ":".
I greatly appreciate any help in getting the leading whitespace to be trimmed properly.