سؤال

So I have a very large project which when run prints a data set that is basicly this:

x,y,z,?,?

I want to find where this is being printed so i can remove this.

I have tried ack-grep for std::cout but there are so many files. Does anyone know how to search for std::cout and 4 commas in a line (ie. i know that std::cout and 4 commas will appear in the line but im not sure what else is there). I thought perhaps there would be some regular expression to use for this, as in find std::cout and "," but four of them.

Any ideas?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

Try cout(.*,){4}.

Note that you may have trouble finding the statement depending upon how the code is written:

char *s="%s,%s,%s,%s";
printf(s,x,y,z,?,?);

or

cout <<
    "x,",
    "y,",
    "z,",
    "?,",
    "?\n";

or any number of similar variations... It might be better to instead look for the routine that generates that kind of output. (Subtle shift, perhaps, but it isn't just looking blindly for the string you've already got.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top