Grep -r (recursive), remove/hide all directories

grep

Noob here – I want to run grep -r asdf however, I only want unique matches in my directories (i.e. disregarding any directory, display unique matches only).

So I ran grep -r asdf | sort --unique. However – this does not work since the directory names are different (dir1/a.txt asdf and dir2/a.txt asdf).

I didn't see an option (I tried e.g. grep -riol) to exclude directories and I guess that barely makes sense for the scope of the function. Can I somehow cut-away the directories and only show the matched filename + match (possibly without a mind/universe-bending regex/sed/…)?

Best Answer

I think with the default capabilities of grep there in no way of doing this.

You could go with something like this, which is just a "small" regex:

grep -r asdf | sed '#^.*/##' | sort --unique

Note: This approach will not work if the search-pattern contains a /

Related Question