LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-30-2005, 07:29 AM   #1
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Rep: Reputation: 15
Question Shell script to find a particular string


Hi all,

Here is something which I am trying to solve. I am trying to find the occurence ofa particular string in the files that I have. I am working in Linux and I need to trace the file which has in it that particular string.

Can you please help me with any particular command which can directly do the same ? It will be of great help if you can help develop the shell script that will do the same thing

Regards
Prasun
 
Old 08-30-2005, 07:43 AM   #2
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 48
Can't you just use grep?

grep <string> <files>

At least in my version of grep, it tells you what file it found the string in when it outputs.
 
Old 08-30-2005, 07:57 AM   #3
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Hi,

Thanks a lot . But what if I have many directories and don't want to enter into each one of them to find out the reqd file which has the particular string. I want a direct command or a general script for that

Regards
Prasun
 
Old 08-30-2005, 08:12 AM   #4
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 48
Grep can do multiple directories - just add "-r", as in:

grep -r <string> <top-level directory>

For example,
Code:
[humphdo1]:~ $ grep -r script tmp/
tmp/a.txt:-rwx------  1 humphdo1 mkgroup_l_d 85 Jul 27 10:17 script
tmp/b.txt:-rwx------  1 humphdo1 mkgroup_l_d  85 Jul 27 10:17 script
tmp/c.txt:-rwx------  1 humphdo1 mkgroup_l_d  85 Jul 27 10:17 script
tmp/d.txt:-rwx------  1 humphdo1 mkgroup_l_d  85 Jul 27 10:17 script
This searches every file in every directory within "tmp/" and tells me that four text files, [a-d].txt contain the word "script"

If you need multiple top-level directories, just write them in one after the other. It's smart enough to understand that.

Check "man grep" for more information on how to use the command.

Last edited by oneandoneis2; 08-30-2005 at 08:14 AM.
 
Old 08-30-2005, 08:37 AM   #5
hpladd
Member
 
Registered: Feb 2003
Distribution: Kubuntu
Posts: 142

Rep: Reputation: 15
I have never been able to receive a "return" by searching the / directory with grep. I assume (perhaps incorrectly) that there is just too much data to search, and I grow impatient and kill the search.

It is best to narrow a grep search to a specific directory. Review the directory tree and search the most probable directories.

For example: If the needed string helps with configuration, search /etc.

I found the following link by searching the web with "my friend" google:
Directory tree

What does the string do?
 
Old 08-30-2005, 09:23 AM   #6
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Hi ,

Thanks a lot . I was just looking for that

Regards
Prasun
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
shell script: grep using string with specail charactors? Chowroc Programming 6 11-14-2005 08:47 PM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
Get video DVD title string for shell script? cheesekeeper Linux - Hardware 9 05-05-2005 05:53 AM
exploding string into individual characters using a shell script farmerjoe Programming 9 10-13-2004 03:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration