user1758367
user1758367

Reputation: 195

Using sed to scan c++ files

I need to find all instances of "[0]" and "[1]" in several files ending in .cpp. The files are located in different sub directories.

Is it poissible to use sed to scan through all .cpp files in sub directories and look for a pattern?

Thanks!

Upvotes: 1

Views: 289

Answers (1)

Phil H
Phil H

Reputation: 20141

Leave sed alone, it's for stream editing, not searching. Use grep.

find . -iname '*.cpp' | while read filename; do grep --with-filename '\[[01]\]' "$filename"; done

Or in a script:

find . -iname '*.cpp' | while read filename 
do 
    grep --with-filename '\[[01]\]' "$filename"
done

I'm using read here (and quotes around $filename) so that filenames with spaces in survive; in Windows systems this is a particular nightmare.

If you know none of your files are particularly big, you can sacrifice efficiency of search for efficiency of typing and search all the files but only pick out the output from cpp files:

grep -r '\[[01]\]' . | grep '.cpp:'

Upvotes: 1

Related Questions