searching inside files with find, cat and grep as a oneliner ...
I need to:
~
search for files using a pattern (say all files with a certain extension)
~
then search inside each of the found files for a word or regexp pattern
~
You could do this using find, cat and grep in a script, but I was
wondering about how could you do it with a oneliner
~
Thanks
lbrtchx
--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: AANLkTike929zPDN58icguTQHXUabe3XuQ5CRZ614Wp_D@mail .gmail.com">http://lists.debian.org/AANLkTike929zPDN58icguTQHXUabe3XuQ5CRZ614Wp_D@mail .gmail.com
09-17-2010, 06:18 PM
Alexander Batischev
searching inside files with find, cat and grep as a oneliner ...
On Fri, Sep 17, 2010 at 06:08:18PM +0000, Albretch Mueller wrote:
> I need to:
> ~
> search for files using a pattern (say all files with a certain extension)
Is this part so complicated that bash can't handle it? I mean, if you need
certain extension, you don't even need cat and find, it's all about grep:
$ grep 'string' *.extension
> ~
> then search inside each of the found files for a word or regexp pattern
But if you really want to use find, here's something you may try:
(-name may be substituted by -iname if you don't want case sensitivity).
This oneliner would work exactly the same as first one - each match would be
preceded with a filename delimited from a matching string itself by a semicolon
(
Hope it helps.
--
Regards,
Alexander Batischev
09-17-2010, 06:20 PM
Camaleón
searching inside files with find, cat and grep as a oneliner ...
On Fri, 17 Sep 2010 18:08:18 +0000, Albretch Mueller wrote:
> I need to:
> ~
> search for files using a pattern (say all files with a certain
> extension)
> ~
> then search inside each of the found files for a word or regexp pattern
> ~
> You could do this using find, cat and grep in a script, but I was
> wondering about how could you do it with a oneliner ~
How about?
***
find /path/to/search/ -type f -iname *.ext -exec grep -H 'text to search' {} ;
***
Greetings,
--
Camaleón
--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: pan.2010.09.17.18.20.37@gmail.com">http://lists.debian.org/pan.2010.09.17.18.20.37@gmail.com
09-17-2010, 06:32 PM
Albretch Mueller
searching inside files with find, cat and grep as a oneliner ...
> if you need certain extension, you don't even need cat and find, it's all about grep:
> $ grep 'string' *.extension
~
The thing is that I need to know in which file the pattern was found
and as you guys suggested:
~
$ find -name '*.extension' -exec grep -H 'pattern' {} ;
~
does it
~
Thanks
lbrtchx
--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: AANLkTi=-WA157uA4xPmq6s-2u7GhX9q3J1NRJqVCC=XB@mail.gmail.com">http://lists.debian.org/AANLkTi=-WA157uA4xPmq6s-2u7GhX9q3J1NRJqVCC=XB@mail.gmail.com
09-17-2010, 06:57 PM
Joe Brenner
searching inside files with find, cat and grep as a oneliner ...
Albretch Mueller <lbrtchx@gmail.com> wrote:
> I need to:
> ~
> search for files using a pattern (say all files with a certain extension)
> ~
> then search inside each of the found files for a word or regexp pattern
> ~
> You could do this using find, cat and grep in a script, but I was
> wondering about how could you do it with a oneliner
http://www.athabascau.ca/html/depts/compserv/webunit/HOWTO/find.htm
Look down to "How to find a string in a selection of files".
http://en.wikipedia.org/wiki/Find
Look down to "Search for a string"
--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 201009171857.o8HIvTsN053020@kzsu.stanford.edu">htt p://lists.debian.org/201009171857.o8HIvTsN053020@kzsu.stanford.edu
09-17-2010, 07:50 PM
Alexander Batischev
searching inside files with find, cat and grep as a oneliner ...
On Fri, Sep 17, 2010 at 06:32:06PM +0000, Albretch Mueller wrote:
> > if you need certain extension, you don't even need cat and find, it's all about grep:
>
> > $ grep 'string' *.extension
> ~
> The thing is that I need to know in which file the pattern was found
> and as you guys suggested:
> ~
> $ find -name '*.extension' -exec grep -H 'pattern' {} ;
> ~
> does it
Um... Well, single grep will show you filename as well, because (quoting the
manpage):
-H, --with-filename
Print the file name for each match. This is the default when there is more
than one file to search.
searching inside files with find, cat and grep as a oneliner ...
On Fri, 17 Sep 2010 14:08:18 -0400 (EDT), Albretch Mueller wrote:
>
> I need to:
> ~ search for files using a pattern (say all files with a certain extension)
> ~ then search inside each of the found files for a word or regexp pattern
> ~ You could do this using find, cat and grep in a script, but I was
> wondering about how could you do it with a oneliner
Search all files under the home directory (recursively) with an extension of .txt
for the keyword "xorg":
grep -r xorg ~/*.txt
--
.'`. Stephen Powell
: :' :
`. `'`
`-
--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 1630941229.67671.1284753074424.JavaMail.root@md01. wow.synacor.com">http://lists.debian.org/1630941229.67671.1284753074424.JavaMail.root@md01. wow.synacor.com
09-18-2010, 05:07 AM
Bob Proulx
searching inside files with find, cat and grep as a oneliner ...
Using {} ; is the old way. That invokes grep once per file. That
works but is slower and less efficient than it could be because it
takes a little bit of time to launch grep.
But newer POSIX standard find can use a {} + to launch grep once and
to pass as many files on the command line as the system allows. That
is faster since grep is launched only as many times as needed.
Usually only once.
As a benefit you don't need to escape the + since it isn't a special
character to the shell.
Bob
09-18-2010, 12:40 PM
Adam Borowski
searching inside files with find, cat and grep as a oneliner ...
On Sat, Sep 18, 2010 at 12:01:17PM +0100, Clive Standbridge wrote:
> > Search all files under the home directory (recursively) with an
> > extension of .txt
> > for the keyword "xorg":
> >
> > grep -r xorg ~/*.txt
>
> That looks like a misunderstanding. That command actually causes grep
> to search
> (a) files matching *.txt in the home directory.
> (b) files of ANY name, contained in subdirectories named *.txt in the
> home directory.
>
> To search all files under the home directory (recursively) with an
> extension of .txt, you will need to use find .. | xargs or find
> .. -exec ... {} + as discussed previously,
I guess you're looking for:
grep -r --include='*.txt' xorg ~
--
1KB // Microsoft corollary to Hanlon's razor:
// Never attribute to stupidity what can be
// adequately explained by malice.
09-18-2010, 03:13 PM
Alexander Batischev
searching inside files with find, cat and grep as a oneliner ...
On Fri, Sep 17, 2010 at 11:07:53PM -0600, Bob Proulx wrote:
> Albretch Mueller wrote:
> But newer POSIX standard find can use a {} + to launch grep once and
> to pass as many files on the command line as the system allows. That
> is faster since grep is launched only as many times as needed.
> Usually only once.
>
> $ find -name '*.extension' -exec grep -H 'pattern' {} +
Wow! I thought that such things can be done only by xargs. Thank you very much!