|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Command to find a string in files
does somebody knows a command to find a string in all the files under a directory (and subdirectories)?
Perhaps a pipe ? With grep mystring filename and find . -name "*.conf" I tried it, but it is not working Thanks for any help JdV |
|
#2
|
||||
|
||||
|
I can't seem to think in shell, but the following Perl lines should work:
Code:
@files = `ls`;
foreach $file (@files) {
system "grep $string $file";
}
__________________
Two things have come out of Berkeley, Unix and LSD. It is uncertain which caused the other. |
|
#3
|
|||
|
|||
|
grep knows the parameter "-r" which is "recursive". Another interesting parameter for searching strings is "-i" which is "case insensitive".
i.e. Code:
# grep -ir "ftp" /var/log searches for the word "ftp" in all logfiles, and reports "ftp", "FTP", "Ftp", ...
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#4
|
||||
|
||||
|
Jose:
There seems to be a little confusion over whether you are looking for a command-line command or a way to do the grep within a program. M.Hirsch's "grep -r" is the way from the command line. From within a program, you could use a pipe: Code:
FILE *fp;
char command[] = "grep -ir \"ftp\" /var/log";
char buffer[PIPE_BUF]; /* PIPE_BUF defined by system */
/* open the pipe to read */
fp = popen(command,"r");
/* read each line output by the command */
while ((fgets(buffer,PIPE_BUF,fp)) != NULL)
{
/* process the line of output */
}
pclose(fp);
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > Linux Help > Command to find a string in files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|