Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Search in all .txt files for a name
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Multimedia
View previous topic :: View next topic  
Author Message
SarahS93
l33t
l33t


Joined: 21 Nov 2013
Posts: 693

PostPosted: Wed Dec 09, 2015 9:17 am    Post subject: Search in all .txt files for a name Reply with quote

with find /mypath/ |grep .txt i find all my text files - there are many text files on my disk.
how can i now search into all these files for a number like 123456789 ?
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Wed Dec 09, 2015 10:01 am    Post subject: Re: Search in all .txt files for a name Reply with quote

SarahS93 wrote:
with find /mypath/ |grep .txt i find all my text files - there are many text files on my disk. how can i now search into all these files for a number like 123456789?

SarahS93 ... you might skip the use of 'find' and use a tool that combines filename and content searches ... of course there are various ways you might achieve the above, find's '-exec', 'grep -R', sys-apps/ack, and no doubt others ... I prefer 'ag' (sys-apps/the_silver_searcher). An example:

ag -G ".*[^/]\.ebuild$" -Q 80211 -l /var/pkg/local/:
/var/pkg/local/net-wireless/wpa_supplicant/wpa_supplicant-2.5-r1.ebuild

... where '-G' is the filename pattern, '-Q' is the string we are searching for, and '-l' is list file (don't print the search string, just the filename that contains the search string).

So, in your test example we might do the following:

Code:
# ag -G ".txt" -Q 123456789 -l /mypath/

... again, there are numerious ways, and tools, you could use, I'm sure others will make suggestions, I use 'ag' as it offers other features (like --file-type, --exclude-dir) which I find useful, and it is fast.

... and btw, you don't need to 'find /mypath | grep .txt', you can use find's '-name', so, again to match your search criteria we might do the following:

Code:
# find /mypath -type f -name "*.txt" -exec grep -l 123456789 {} \;

best ... khay

edit: typos!!


Last edited by khayyam on Wed Dec 09, 2015 10:34 am; edited 2 times in total
Back to top
View user's profile Send private message
Syl20
l33t
l33t


Joined: 04 Aug 2005
Posts: 619
Location: France

PostPosted: Wed Dec 09, 2015 10:22 am    Post subject: Reply with quote

I didn't know ag. It seems to be available on many distros, that's good. :)

Without it, you can simply use :
Code:
find /mypath/ -name "*.txt" -exec grep 123456789 {} \;


But you won't get the relative path of the files with this command. Just their name. You can add "-ls" too.
Code:
find /mypath/ -name "*.txt" -ls -exec grep 123456789 {} \;
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Wed Dec 09, 2015 10:38 am    Post subject: Reply with quote

CneGroumF wrote:
But you won't get the relative path of the files with this command. Just their name. You can add "-ls" too.

Code:
find /mypath/ -name "*.txt" -ls -exec grep 123456789 {} \;

CneGroumF ... yes, but that will return the list of files found by find, not those returned by grep ... you can use 'grep -l' in the above rather than finds '-ls'.

Code:
$ find /mypath/ -name "*.txt" -exec grep -l 123456789 {} \;

best ... khay
Back to top
View user's profile Send private message
Syl20
l33t
l33t


Joined: 04 Aug 2005
Posts: 619
Location: France

PostPosted: Wed Dec 09, 2015 10:44 am    Post subject: Reply with quote

Yes, that's better. Thank you khayyam.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Wed Dec 09, 2015 10:50 am    Post subject: Reply with quote

SarahS93, CneGroumF, et al ...

... and btw, you can also use zsh's shell globbing and 'glob qualifiers' to do the same as find ...

Code:
% grep -l 123456789 /mypath/**/*.txt*(.)

... not all shells are created equal, bash users need not apply ;)

best ... khay
Back to top
View user's profile Send private message
cboldt
Veteran
Veteran


Joined: 24 Aug 2005
Posts: 1046

PostPosted: Tue Dec 22, 2015 10:19 pm    Post subject: Reply with quote

I'd just use the "-r" (recursive) switch on grep, and "*.txt" glob to limit the scope of files searched.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Tue Dec 22, 2015 11:43 pm    Post subject: Reply with quote

cboldt wrote:
I'd just use the "-r" (recursive) switch on grep, and "*.txt" glob to limit the scope of files searched.

cboldt ... that's basically what the above does, though the recursive is globbed. The difference is that with glob qualifiers you can do more specific matches:

Code:
% grep -l 123456789 ^(dir1|dir2)**/*.txt*(mM+12)

So, recursive, excluding dir1 and dir2, for files with the suffix .txt and having been modified over twelve months ago.

Or, say we don't know the filename or suffix, but we know it's a python script:

Code:
% grep -l 123456789 **/*(e:'file $REPLY | grep -q "python script"':)

Glob qualifiers allow us to be more specific about the match, and can be used in any circumstance in which a glob might be applied.

best ... khay
Back to top
View user's profile Send private message
cboldt
Veteran
Veteran


Joined: 24 Aug 2005
Posts: 1046

PostPosted: Wed Dec 23, 2015 10:00 am    Post subject: Reply with quote

`grep -r 0123456789 *txt` applies a recursive (*txt) glob too, from whichever directory the command is invoked from.

That said, I do see the value in limiting the search in the ways you outline, excluding certain directories and files older (or newer) than some selected date; as well as the value in being able to search based on filetype, independent of file extension. Those are powerful features.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Multimedia All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum