You are not logged in.

#1 2009-05-03 12:58:11

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,592
Website

help requestion: addition to .bashrc for searching within files

I'I know the following command can be used to search within files for a text string.  In my example, I'm looking for the word scenario:

First cd into my ~/docs and do:

find ./ -exec grep -q 'scenario' '{}' \; -print

What I'd like to do is have this in my ~/.bashrc such that I can call an aliase to look for a word or phrase within all the files in my current directory.  I just don't know how to do this.  Can anyone help me or if this is a totally ridiculous idea and if there is some better way, please let me know as well.

Thanks!

Last edited by graysky (2009-05-03 12:59:16)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2009-05-03 13:03:31

dimigon
Member
Registered: 2009-03-07
Posts: 139
Website

Re: help requestion: addition to .bashrc for searching within files

graysky wrote:

I'I know the following command can be used to search within files for a text string.  In my example, I'm looking for the word scenario:

First cd into my ~/docs and do:

find ./ -exec grep -q 'scenario' '{}' \; -print

What I'd like to do is have this in my ~/.bashrc such that I can call an aliase to look for a word or phrase within all the files in my current directory.  I just don't know how to do this.  Can anyone help me or if this is a totally ridiculous idea and if there is some better way, please let me know as well.

Thanks!

Try to use "grep -inr pattern ."

so if you want to search inside the directory ./code for main do grep -inr main ./code smile

Offline

#3 2009-05-03 13:46:28

cytzol
Member
Registered: 2007-06-27
Posts: 10

Re: help requestion: addition to .bashrc for searching within files

Try ack:

$ ack scenario
(shows you all code files in the current directory (recursively) containing "scenario")

$ ack -a scenario
(the same, but searching all files)

$ ack --text scenario
(the same, but only searching text files)

Offline

#4 2009-05-04 00:21:37

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: help requestion: addition to .bashrc for searching within files

graysky wrote:

I'I know the following command can be used to search within files for a text string.  In my example, I'm looking for the word scenario:

First cd into my ~/docs and do:

find ./ -exec grep -q 'scenario' '{}' \; -print

What I'd like to do is have this in my ~/.bashrc such that I can call an aliase to look for a word or phrase within all the files in my current directory.  I just don't know how to do this.  Can anyone help me or if this is a totally ridiculous idea and if there is some better way, please let me know as well.

Thanks!

Not ridiculous at all. Might be pretty darn useful actually.

alias search='find ./ -exec grep -q ${1} '{}' \; -print'

I think that's what your actually after.
The just do

search something

to return your results. If looking for more than one word do the same with quotes when searching.

search "somthing and some more words"

Last edited by crouse (2009-05-04 00:24:11)

Offline

#5 2009-05-04 06:24:56

klixon
Member
From: Nederland
Registered: 2007-01-17
Posts: 525

Re: help requestion: addition to .bashrc for searching within files

I don't think aliases can expand variables. You'd have to make it a function:

function search()
{
  PATTERN="$*"
  [[ -z ${PATTERN} ]] && echo Please provide a pattern to search && exit 1
  find ./ -exec grep -q "${PATTERN}" '{}' \; -print
}

Stand back, intruder, or i'll blast you out of space! I am Klixon and I don't want any dealings with you human lifeforms. I'm a cyborg!

Offline

#6 2009-05-12 22:28:28

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: help requestion: addition to .bashrc for searching within files

+1 to klixon for it's (do cyborgs have sex/gender?) remark about aliases.

@dimigon
Have you used your "grep -inr pattern ." on a big dir? Do you think you have enough RAM to handle it? I have only 1G so in my test case, after 30sec. swap come into play and after a minute the script crashed w/ "Out of memory" error. It's a nice way of getting rid of this annoying cache in RAM ;-)

@graysky
Why an alias and not a one-line script (two-line if you count #!/bin/bash) you can put in $PATH? After some time you may gather a couple more handy scripts and reorganize them into a library-type script w/ getopts handling switches indicating which script you want to invoke.

If the dirs you're grepping may have binary data you may want to use -I switch to tell grep to ignore such files.
Do you want to hardcode the dir you're searching in? If not, you need two vars: one for "where" and the other for "what".

I find (pun intended)

find . -type f | xargs grep -iI "ArchLinux"

better suited for *my* needs - *you* may want to tweak it to taste.

For "needle in a haystack" searches when you're looking for _that_ particular line you know it is somewhere "there/", I'm getting the feeling that the above line is I/O bound on my box.
I find it more portable than Beagle, but doubt it scales as well. For searching my 8G kingdom for some function in a piece of code it's good enough as it executes in around 30sec.

Offline

#7 2009-05-12 22:31:38

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: help requestion: addition to .bashrc for searching within files

i dunno bout you guys but i just use fgrep, maybe i'm missing something?

> cd /etc/
> fgrep DAEMONS ./*
mdadm.conf:# "mdadm" to your DAEMONS array in /etc/rc.conf
rc.conf:# DAEMONS
rc.conf:DAEMONS=(syslog-ng network @sensors @iptables @hal @skvm @dropboxd @fcron @openntpd @oss !mysqld @sshd @httpd !vsftpd @samba @ushare @cups @icecast @mpd)
rc.multi:for daemon in "${DAEMONS[@]}"; do
rc.shutdown:    let i=${#DAEMONS[@]}
rc.shutdown:            if [ "${DAEMONS[$i]:0:1}" != '!' ]; then
rc.shutdown:                    ck_daemon ${DAEMONS[$i]#@} || stop_daemon ${DAEMONS[$i]#@}
rc.single:      let i=${#DAEMONS[@]}
rc.single:              if [[ $(echo ${DAEMONS[$i]} | /bin/grep '^[^\!]' | /usr/bin/wc -l) -eq 1 ]]; then
rc.single:                      /etc/rc.d/${DAEMONS[$i]#@} stop

Offline

#8 2009-05-12 22:43:41

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: help requestion: addition to .bashrc for searching within files

> i dunno bout you guys but i just use fgrep, maybe i'm missing something?
Recursion?

Offline

#9 2009-05-12 23:53:29

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: help requestion: addition to .bashrc for searching within files

karol wrote:

> i dunno bout you guys but i just use fgrep, maybe i'm missing something?
Recursion?

well then...

> fgrep something ./*/*/*/*

wink

Offline

#10 2009-05-12 23:56:20

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: help requestion: addition to .bashrc for searching within files

brisbin33, you owe me a beer, I've just spilled like half of mine when I read your "response" :-)

Offline

Board footer

Powered by FluxBB