Jump to content

Searching through files and displaying results


Bumblebee08

Recommended Posts

I'm trying to search for multiple words inside multiple files. Right now I get a nice result list of the keywords I wanted to find using grep, but without filenames. I want to know the source and context of the keywords I'm looking for to do some deeper digging. I can also 'grep -Ril', but then I just get the filenames the words are found in without the actual words (not really useful when searching for multiple words).

Does anyone have any tips about how I can combine searching for multiple keywords within files and how to display the filenames these words were found in, on the same line (even if the words were found in more then one file). It would be nice to have a txt file as input for the keywords I'm looking for, but I guess I need to do some python scripting then?

Edited by Bumblebee08
Link to comment
Share on other sites

Please see https://www.blackmoreops.com/2014/08/20/find-files-containing-specific-text/

I was under the impression that searching for text within files, using grep, listed the names of the files already.

grep -r "SEARCHTERM" /*

should output...
 

	grep -r "SEARCHTERM" /*
/file1.txt
/file2.doc
/file3.log

etc...

Edited by haze1434
Link to comment
Share on other sites

Have you tried using something like:

grep -orEH "(sub|new)" * | sed -re's|^([^:]*):(.*)$|\2:\1|' | sort

The sed part switches the file name and matched keyword round so that the sort can then sort the output into keyword order. If you don't want duplicates to appear then simply pipe the output through uniq (if you supply uniq with the -c option then it'll give you count of how many times that keyword appears in that file).

Link to comment
Share on other sites

Thanks for the replies!

@Haze1434, I tried the commands you mentioned and it worked of course. I got to see the filename and the keyword I was looking for.

# grep -r "keyword" *
/home/user/testfile.txt: keyword

I should have mentioned that I´m trying to search for specific keywords in pcap files on an Ubuntu system. I found out that I should have used the -a option with grep, to process a binary file as if it were text.

# grep -ar "keyword-to-search-for\|second-keyword" *
Binary file test1.pcap matches
Binary file test2.pcap matches
testfile1.txt: keyword-to-search-for
testfile2.pcap: second-keyword

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...