LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to search for specific pattern? (https://www.linuxquestions.org/questions/programming-9/bash-script-to-search-for-specific-pattern-553597/)

jimyg10 05-13-2007 04:43 PM

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...

Dark_Helmet 05-13-2007 04:52 PM

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.

jimyg10 05-13-2007 05:26 PM

I work in minix3 and the command grep has only -e-l-n-s-v.., hasn't it??

cfaj 05-13-2007 05:31 PM

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" {} \;

jimyg10 05-13-2007 05:39 PM

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"...

jimyg10 05-14-2007 02:43 AM

Anyone knows why???

makyo 05-14-2007 05:57 AM

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

jimyg10 05-14-2007 07:48 AM

Thnx makyo.. But i have problem again.. It returns to me "Out of memory"...:confused: :confused:
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...

makyo 05-14-2007 08:54 AM

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

ghostdog74 05-14-2007 09:36 AM

Quote:

Originally Posted by jimyg10
Thnx makyo.. But i have problem again.. It returns to me "Out of memory"...:confused: :confused:
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"

jimyg10 05-14-2007 09:37 AM

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??

ghostdog74 05-14-2007 09:44 AM

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


jimyg10 05-14-2007 10:29 AM

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..

ghostdog74 05-14-2007 10:56 AM

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?

jimyg10 05-14-2007 12:49 PM

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..


All times are GMT -5. The time now is 11:06 PM.