Solved

Linux: Find files containing text regardless of dashes

Posted on 2014-03-31
6
369 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 28

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
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 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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

920 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

15 Experts available now in Live!

Get 1:1 Help Now