Solved

Linux: Find files containing text regardless of dashes

Posted on 2014-03-31
6
366 Views
Last Modified: 2014-04-01
Running Linux, how can I find all files containing specific text, disregarding dashes?

For example:
grep -iwr "hello-world" /my/dir/

Open in new window

That should find a file that contains this:
Hello World
?

Also, I only need the filename.  I do not want content from the file.

Thanks!
0
Comment
Question by:hankknight
6 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 39967065
It slightly depends on what you are looking for.  If you want any single character between the "hello" and the "world", use a dot (so "hello.world")  - dot matches any character - that would match "hello world", "hello-world", or even "hello%world".

If you want a specific set of allowed characters, use something like "hello[- _]world", to allow hyphen, space or underscore (in this example).  Normally a hyphen in a character class like that (i.e. between "[" and "]") would indicate a range of values (e..g "[A-Z]" means all of the upper-case letters), but at the start or end of the class (next to the "[" or "]") it matches a hyphen.

To get just the names, add the "-l" flag to your "-iwr".
0
 
LVL 27

Expert Comment

by:serialband
ID: 39967349
If you want to match capitals and lower case you'd do the following

grep -liwr [Hh]ello.[Ww]orld  /my/dir/

If you want it to match the space character, you either quote the string or escape the space
grep -liwr "[Hh]ello [Ww]orld"  /my/dir/
grep -liwr [Hh]ello\ [Ww]orld  /my/dir/
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39967407
(there's a -i in the options, so case is ignored anyway)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:serialband
ID: 39967766
Yes, I saw that, yet I continued posting.  :P
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39967905
How are you deriving your search string?
0
 
LVL 3

Expert Comment

by:Kevin Pham
ID: 39969907
Why would you want to add a dash to the text string... then write a script to strip it? Just do this

$ grep -Ril 'hello world' /my/dir

This will look recursively to find the words and only display the filenames.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now