I am on macOS and would like to get into using grep
(or a similar tool) to find unique occurrences of a certain pattern in a codebase. For example, for finding all console.somemethod()
calls in JavaScript I have devised:
grep -oiER "console\.([a-z]+)\(" . | sort -u
But this gets me results in the form:
./tools/svg-inject/node_modules/with/node_modules/acorn/src/bin/acorn.js:console.log(
./tools/svg-inject/node_modules/wordwrap/README.markdown:console.log(
./tools/svg-inject/node_modules/wordwrap/example/center.js:console.log(
./tools/svg-inject/node_modules/wordwrap/example/meat.js:console.log(
./tools/svg-inject/node_modules/yargs/README.md:console.dir(
./tools/svg-inject/node_modules/yargs/README.md:console.log(
./tools/svg-inject/node_modules/yargs/index.js:console.log(
./tools/svg-inject/node_modules/yargs/lib/usage.js:console.error(
./tools/svg-inject/node_modules/yargs/lib/usage.js:console.log(
./webpack.config.js:console.info(
Console.sendTo(
console.error(
console.log(
console.markTimeline(
console.reactStackEnd(
console.timeEnd(
console.trace(
console.warn(
I would like to restrict it to unique matches of the ([a-z]+)
group only:
info
sendTo
error
log
markTimeline
reactStackEnd
timeEnd
trace
warn
Apologies if I'm rehashing an old question!
sed
for this sort of thing, but it works only with a specific file list (no-R
option). You can pipe the existinggrep
output throughsed
, but this gives no advantage over piping throughperl
or a secondgrep
, as in @Toto's answer. If you know the depth of the directory tree, you can usesed -n 's/^.*console\.\([a-z]+\)(.*$/\1/p' * */* */*/* */*/*/*|sort -u
(for three levels of subdirectory). It's worth getting to knowsed
, and this is a good introduction.