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

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.

1
  • Oh, the issue was that it's either a space or a tab. So I ended up with : awk -F': |:\t' – Bastien974 Aug 26 '16 at 17:00
1

Try this:

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

4
  • Still getting the whole line. – Bastien974 Aug 26 '16 at 16:53
  • And changing \t for an actual tab character? – NuTTyX Aug 26 '16 at 16:54
  • ?! tab button doesn't do that in a shell and in a text editor it depends on which column I am. – Bastien974 Aug 26 '16 at 16:58
  • Doesn't do what? Typing a tab does work both in a shell and in a text editor. If it doesn't work, just use -F ':' and trim leading spaces with sed | sed 's/^[ \t]*//' – NuTTyX Aug 26 '16 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.