FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 09-17-2010, 06:08 PM
Albretch Mueller
 
Default 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
 
Old 09-17-2010, 06:18 PM
Alexander Batischev
 
Default 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:

$ find -name '*.extension' -exec grep -H 'pattern' {} ;

(-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
 
Old 09-17-2010, 06:20 PM
Camaleón
 
Default 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
 
Old 09-17-2010, 06:32 PM
Albretch Mueller
 
Default 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
 
Old 09-17-2010, 06:57 PM
Joe Brenner
 
Default 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"

But the newer style is to pipe find into xargs:

http://blog.endpoint.com/2010/07/efficiency-of-find-exec-vs-find-xargs.html
http://www.sunmanagers.org/pipermail/summaries/2005-March/006255.html
http://www.unix.com/unix-dummies-questions-answers/19217-difference-between-xargs-exec.html


--
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
 
Old 09-17-2010, 07:50 PM
Alexander Batischev
 
Default 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.

--
Regards,
Alexander Batischev

1024D/69093C81
F870 A381 B5F5 D2A1 1B35 4D63 A1A7 1C77 6909 3C81
 
Old 09-17-2010, 07:51 PM
Stephen Powell
 
Default 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
 
Old 09-18-2010, 05:07 AM
Bob Proulx
 
Default searching inside files with find, cat and grep as a oneliner ...

Albretch Mueller wrote:
> $ find -name '*.extension' -exec grep -H 'pattern' {} ;

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.

$ find -name '*.extension' -exec grep -H 'pattern' {} +

As a benefit you don't need to escape the + since it isn't a special
character to the shell.

Bob
 
Old 09-18-2010, 12:40 PM
Adam Borowski
 
Default 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.
 
Old 09-18-2010, 03:13 PM
Alexander Batischev
 
Default 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!

--
Regards,
Alexander Batischev

1024D/69093C81
F870 A381 B5F5 D2A1 1B35 4D63 A1A7 1C77 6909 3C81
 

Thread Tools




All times are GMT. The time now is 06:08 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org