Pipe grep result to awk with “: ” as field separator

awkgrep

I would like to filter the result of a grep command, eg :

myRepo/path/to/my/file.php:123:                             error_log(' - myError');

If I do the following, it works.

echo " myRepo/path/to/my/file.php:123: error_log(' – myError');" | awk -F': ' '{print $1}'

But when it's the result of a grep command, it outputs the whole line, why ?

grep -rn "myError" | awk -F': ' '{print $1}'

I have CentOS 6 with Awk 3.1.7, bash 4.1.2

Best Answer

Your awk delimiter is a : and a space, but I would guess that your code is using tab for indentation (and hence does not match just on a space).

You can try to change your grep to the following (which causes awk to match on : and the tab character).

grep -rn "myError"|awk -F':\t' '{ print $1 }'

This works for me using bash 4.3.46 and awk 4.1.3 on slackware linux.