Solved

Linux: Find files containing text regardless of dashes

Posted on 2014-03-31
6
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 29

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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.:

726 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