2

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

Bastien974
  • 1,896
  • 12
  • 44
  • 62

2 Answers2

1

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.

halldorhaf
  • 61
  • 3
1

Try this:

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

NuTTyX
  • 1,168
  • 5
  • 10