I want to filter out lines matched by grep such that lines longer than some maximum length are removed from match results.
An example use case for this is using grep
to find matches for a particular function call. In many cases, particularly if the source code is something like python
, a function name might be fairly generic leading to a large number of matches.
In some cases, these matches will be bogus, and very lengthy. (Meaning that each match is a line of thousands of characters which fills the terminal screen and obscures the results.
One such example are matches whcih come from python/NodeJS/etc source code, where the matches come from imported packages, rather than the source code the developer is authoring.
If I grep
for matches without filtering long lines, I get a wall of text due to spurious matches. As mentioned, this is because some of the matched lines are multiple thousands of characters in length. It would be useful to be able to filter these out, possibly using grep
.
The important grep
string is grep '^\{1,200\}$'
, which means:
^
anchor beginning of line$
anchor end of line.\{1,200\}
match any character (.
) from 1 to 200 timesgrep -rI some_function . 2>/dev/null | grep '^.\{1,200\}$'
This will filter out lines which are not between 1 and 200 characters in length.
Since most source code lines are typically a lot less than 200 characters (120 is a typical upper limit) this is ideal for filtering out matches which are unlikely to be lines of source code which has been written by a human programmer rather than some automated process.