Solved

Linux: Find files containing text regardless of dashes

Posted on 2014-03-31
6
376 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can't connect to FTP 18 154
rsyslog raw message 4 60
Use awk or sed to manipulate Columns in a File 12 45
[bash] test if filenames  are same 2 14
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

734 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