Agreed pretty much on the approach of
ack 'cout.+(.+,){4,}' --cpp
Note that it's .+
not .*
, because .*
will match a null string, so ,,,,
would match. You want to make sure you have something in between the commas. I also added the --cpp
flag so that it only selects C++ files to search through.
Now, you said you wanted to delete every line that matches, so here's how you can extend this solution to do that. Of course, you'll want to make sure you have all you source code in version control, or at the very least have a backup, in case something goes wrong. I can't tell exactly what your code looks like. You'll have to do all the checking that the code works in your case.
If you've found that every line that ack finds is one you want to delete, you can use Perl to remove those lines. The regex you use is the same because ack is in Perl and uses Perl regexes.
ack -f --cpp | xargs perl -i -n -e'print unless /cout.+(.+){4,}/'
The ack -f --cpp
tells ack to generate a list of all the C++ files in the tree, and then xargs calls that Perl command line repeatedly. perl's -i
option says "Edit file in place", -n
tells Perl to loop over an input file, and -e
gives the command to execute for each line. In this case, we're telling Perl to print whatever line it just read unless it matches that regex. This effectively deletes the matching lines from each file.