Simple grep and search & replace

Posted on Saturday, July 5th, 2008 at 8:36 pm in

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

You might also be interested in

Add your comment

Leave a Reply

Leave A Comment

Top