LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-13-2007, 04:43 PM   #1
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Rep: Reputation: 0
Bash script to search for specific pattern?


Hello guys..

I want your help.. I want to make a bash script (named *.sh), that will search all the files in all the directories under the current path for a specific pattern. The result would be

filename:linenumber:line that has the specific pattern
filename:linenumber:line that has the specific pattern
filename:linenumber:line that has the specific pattern
...

Anybody???
Thanks in advance...
 
Old 05-13-2007, 04:52 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
The man pages are your friend: man grep

Read the text on options '-H' '-n' and '-r' -- you don't need a script; just a single command.
 
Old 05-13-2007, 05:26 PM   #3
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
I work in minix3 and the command grep has only -e-l-n-s-v.., hasn't it??
 
Old 05-13-2007, 05:31 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jimyg10
I work in minix3 and the command grep has only -e-l-n-s-v.., hasn't it??

What does your man page say?

Code:
find . -type f -exec grep -n /dev/null "$pattern" {} \;
 
Old 05-13-2007, 05:39 PM   #5
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by cfaj

What does your man page say?
it says only -e-l-n-s-v...the -r -H??

Quote:
Originally Posted by cfaj

Code:
find . -type f -exec grep -n /dev/null "$pattern" {} \;
it returns "find: -exec/-ok: ;Missing"...
 
Old 05-14-2007, 02:43 AM   #6
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Anyone knows why???
 
Old 05-14-2007, 05:57 AM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

The "/dev/null" string (to force the display of the filename) appears to be out of order. This arrangement worked for me:
Code:
find . -type f -exec grep -n "$pattern" {} /dev/null \;
Best wishes ... cheers, makyo
 
Old 05-14-2007, 07:48 AM   #8
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Thnx makyo.. But i have problem again.. It returns to me "Out of memory"...
it's ok if i replace "$pattern" with a specific pattern (example "echo")..

I have another question..
How can i insert this command in a bash script named find.sh??
i want the user to enter: find.sh + pattern (for example find.sh echo) and the script to return the results..

Thnx in advance...

Last edited by jimyg10; 05-14-2007 at 07:56 AM.
 
Old 05-14-2007, 08:54 AM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.
Quote:
Originally Posted by jimyg10
... How can i insert this command in a bash script named find.sh??
i want the user to enter: find.sh + pattern (for example find.sh echo) and the script to return the results..
Quote:
The positional parameters are the words following the name of a shell script. They are put into the variables $1, $2, $3 and so on. As long as needed, variables are added to an internal array. $# holds the total number of parameters, as is demonstrated with this simple script:

-- more at http://www.tldp.org/LDP/Bash-Beginne...ect_03_02.html
Once you have read that document, also visit and bookmark http://www.tldp.org/LDP/abs/html/index.html for other questions ... cheers, makyo
 
Old 05-14-2007, 09:36 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jimyg10
Thnx makyo.. But i have problem again.. It returns to me "Out of memory"...
it's ok if i replace "$pattern" with a specific pattern (example "echo")..
this should be more efficient
Code:
find . -type f -print0 | xargs -0 grep -n "$pattern"
 
Old 05-14-2007, 09:37 AM   #11
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
I've done this:

Code:
ed
a
pattern="$1"
find . -type f -exec grep -n "$1" {} /dev null \;
.
w test.sh
q
and then
Code:
chmod 755 test.sh
it's ok..Is there an other way to do the same job??
 
Old 05-14-2007, 09:44 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jimyg10
I've done this:

Code:
ed
a
pattern="$1"
find . -type f -exec grep -n "$1" {} /dev null \;
.
w test.sh
q
and then
Code:
chmod 755 test.sh
it's ok..Is there an other way to do the same job??
if you want to generate it dynamically another way:
Code:
cat << EOF > "find.sh"  
pattern="$1"
find . -type f -exec grep -n "$1" {} /dev null \;
EOF
 
Old 05-14-2007, 10:29 AM   #13
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you all guys..

Now i have a few more general questions:

a. How can i create a sorted list with the online users?
b. How can i get all the files, whose names ends with c or h?
c. How can i export all current processes in a .txt file and simultanusly on the screen?
d. How can i get these processes sorted by their size on the screen?

Thnx in advance whoever could help..
 
Old 05-14-2007, 10:56 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jimyg10
Thank you all guys..

Now i have a few more general questions:

a. How can i create a sorted list with the online users?
b. How can i get all the files, whose names ends with c or h?
c. How can i export all current processes in a .txt file and simultanusly on the screen?
d. How can i get these processes sorted by their size on the screen?

Thnx in advance whoever could help..
giving you the benefit of the doubt that these are homework:
Quote:
a. How can i create a sorted list with the online users?
something in the likes of ...
Code:
who | sort <options>
Quote:
b. How can i get all the files, whose names ends with c or h?
use find
Code:
find /path -type f -name "*.c" -name "*.h"
Quote:
c. How can i export all current processes in a .txt file and simultanusly on the screen?
to list processes, use ps. to print to screen as well as into file, use tee
Code:
ps | tee file
Quote:
d. How can i get these processes sorted by their size on the screen?
what size?
 
Old 05-14-2007, 12:49 PM   #15
jimyg10
LQ Newbie
 
Registered: May 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74
giving you the benefit of the doubt that these are homework:


use find
Code:
find /path -type f -name "*.c" -name "*.h"
if i don't want the file extension to be c or h, but the name of the file??

thnx for alla the answers ghostdog74.. By the way, it's not homework..
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash search for a pattern within a string variable nutthick Programming 7 03-06-2012 03:54 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
bash script pattern matching thedude2010 Programming 9 06-02-2006 02:39 AM
Help working on a script to search for specific data. oracle11112 Programming 26 05-28-2005 07:00 PM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM


All times are GMT -5. The time now is 11:57 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration