Simple grep and search & replace

grep -Hn -e ” int” *.c* *.h

searches for the string “int” files ending in .c* or .h in the the current directory directory

Returns:

! P8.CPP:52: cerr << “cannot allocate int *p1″ << endl ;
! P8.CPP:59: } //format => int *p = new int[100];
! P9.CPP:9:inline int sumup( int x, int y)
! P9.CPP:17: int i1 = 10, i2 = 20, sum = 0;
! functions.h:3:int doTotal(int x1, int x2)
! functions.h:12:float doAverage(int x1, int x2)
! functions.h:19:int doDifference(int x1, int x2)

In case you want to search through some text files in a series of
directories replacing one set of text for another in each of the files,
try this shell script.

#!/bin/sh
for file in `grep -liR “someword” ./*`;
do
sed ‘s/someword/someother_word/g’ $file > tmp/$$ && mv tmp/$$ $file
done

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>