ctxgrep
v0.0.3
Published
Context-aware regex matching
Downloads
2
Readme
Context-aware grep
This is a simple regex match CLI that includes all lines leading up to a match that have a lower indentation than the match itself. It has far fewer features than GNU grep. In fact, it's usage is very simple:
cat file | ctxgrep expression
or
ctxgrep expression [filename|-]
where -
causes ctxgrep
to read from stdin
.
Example
Which dependencies of apollo-server match the expression graphql
?
yarn list
would tell us, but would not filter. grep
would filter, but would
not tell us the path down the dependency tree that causes the dependency. So,
we replace the box drawing characters that yarn list
outputs with spaces, so
we can properly count indentation, and filter with ctxgrep
. This tells us not
only which dependencies match graphql
, but also the branch of the tree that
causes the dependency.
$ yarn list | | ctxgrep -b graphql
stdin: 1:yarn list v1.22.19
stdin: 5: @apollo/[email protected]
stdin: 6: apollo-graphql@^0.4.0
stdin: 10: @apollo/[email protected]
stdin: 22: apollo-graphql@^0.4.0
stdin: 49: graphql-extensions@file:packages/graphql-extensions
stdin: 101: @apollographql/[email protected]
stdin: 103: @apollographql/[email protected]
stdin: 967: @types/[email protected]
stdin: 972: [email protected] - 16
stdin: 973: [email protected]
stdin: 1180: [email protected]
stdin: 1182: graphql-extensions@file:packages/graphql-extensions
stdin: 1194: [email protected]
stdin: 1196: apollo-graphql@^0.4.0
stdin: 1202: graphql-extensions@file:packages/graphql-extensions
stdin: 1226: [email protected]
stdin: 1245: [email protected]
stdin: 1246: @apollographql/[email protected]
stdin: 1251: graphql-tools@^4.0.0
stdin: 1252: [email protected]
stdin: 1285: [email protected]
stdin: 1286: @apollographql/[email protected]
stdin: 1290: graphql-tools@^4.0.0
stdin: 1291: [email protected]
stdin: 1301: [email protected]
stdin: 1302: @apollographql/apollo-tools@^0.4.3
stdin: 1303: @apollographql/[email protected]
stdin: 1305: @apollographql/[email protected]
stdin: 1306: @types/graphql-upload@^8.0.0
stdin: 1332: graphql-extensions@file:packages/graphql-extensions
stdin: 1333: graphql-tag@^2.9.2
stdin: 1334: [email protected]
stdin: 1336: graphql-tools@^4.0.0
stdin: 1337: [email protected]
stdin: 1343: graphql-upload@^8.0.2
stdin: 1376: [email protected]
stdin: 1377: @apollographql/[email protected]
stdin: 1473: graphql-subscriptions@^1.0.0
stdin: 1474: [email protected]
stdin: 1476: graphql-tools@^4.0.0
stdin: 1477: [email protected]
stdin: 1516: [email protected]
stdin: 1517: @apollographql/[email protected]
stdin: 1522: graphql-subscriptions@^1.0.0
stdin: 1523: [email protected]
stdin: 1525: graphql-tools@^4.0.0
stdin: 1526: [email protected]
stdin: 1532: [email protected]
stdin: 1533: @apollographql/[email protected]
stdin: 1538: graphql-subscriptions@^1.0.0
stdin: 1539: [email protected]
stdin: 1541: graphql-tools@^4.0.0
stdin: 1542: [email protected]
stdin: 1551: [email protected]
stdin: 1552: @apollographql/[email protected]
stdin: 1563: graphql-subscriptions@^1.0.0
stdin: 1564: [email protected]
stdin: 1566: graphql-tools@^4.0.0
stdin: 1567: [email protected]
stdin: 1579: [email protected]
stdin: 1580: @apollographql/[email protected]
stdin: 1586: graphql-tools@^4.0.0
stdin: 1587: [email protected]
stdin: 1593: [email protected]
stdin: 1594: @apollographql/[email protected]
stdin: 1611: [email protected]
stdin: 1670: graphql-subscriptions@^1.0.0
stdin: 1671: [email protected]
stdin: 1673: graphql-tools@^4.0.0
stdin: 1674: [email protected]
stdin: 1708: [email protected]
stdin: 1710: graphql-extensions@file:packages/graphql-extensions
stdin: 2852: [email protected]
stdin: 2853: @apollographql/apollo-tools@^0.4.3
stdin: 2854: @apollographql/[email protected]
stdin: 2879: [email protected]
stdin: 2881: [email protected]
stdin: 2882: [email protected]
stdin: 2888: [email protected]
stdin: 2901: [email protected]
It's also useful for grepping code, because, for a given variable name, it will tell you the if-statement in which it is read/written, all the way up to the name of the function wherein the if-statement appears. Since it calculates context from the initial indentation and nothing more, it does not track multiline function definitions. So, a function definition like
int main(
int argc,
char **argv
) {
int variable_name = 0;
}
would only show
) {
int variable_name = 0;