问题
I recently switched from a Windows development environment to an Apple development environment. The move has been a challenging process, but I'm struggling with picking up UNIX based commands in terminal to makeup for the commands I used on a daily basis in Windows command prompt. Any help would greatly appreciated, explanations on a basic level of what's going on in the commands provided is a huge bonus for me as I'm trying to get a grasp on the UNIX commands, but reading the manual is like reading a foreign language to me most of the time.
Here's my question: Is there a single line command, preferably short enough to memorize, that I can execute to mimic or produce very similar output to the following Windows CMD command:
findstr /s /c:"this piece of text" *.code
I use this command on Windows often to produce a result set that shows me where the text between the quotes resides in any of the files matching the *.code pattern in any subdirectories. This can be used to check version numbers of numerous files pulled back from servers to looking for where a variable was declared in a large project. The output comes in this form:
file1.code: other text this piece of text other text
file2.code: other text this piece of text other text
file3.code: other text this piece of text other text
file4.code: other text this piece of text other text
file5.code: other text this piece of text other text
Where other text is any other text found on the same line as my search string in the given file. I have searched through the questions here and found several people using find . -name *.code to build a list of files in the subdirectories. They then use the -exec flag from the find command paired with a grep sequence to search text. I tried this in several of the mentioned ways and was failing, I think due to escape sequences or missed characters. It would be awesome if there was a way to just give the command a string in between quotes that it just searched for as is.
I tried the following and wasn't getting any results... Maybe a syntax error?
find . -exec grep -H .getItemToChange().getItemAttributes()
UPDATE The correct code is provided below with a great explanation by John. If this helps you like it helped me give his answer an upvote!
I was hoping to find the .java file with this function call in it in a large project. It wasn't giving me any results and it also didn't have a way to filter to only *.java.
Can anyone help me out here? Explanations to your commands are GREATLY appreciated.
回答1:
find . -name '*.code' -exec grep -H 'pattern' {} +
Make sure to quote '*.code' so the shell doesn't expand the * wildcard. Usually we do want the shell to do the expansion, but in this case we want the literal string *.code to be passed to find so it can do the wildcard expansion itself.
When you use -exec you need to put {} somewhere; it's the placeholder for the file names that are found. You also need either \; or + at the end of the command. It's how you signal to find where the end of -exec's arguments are (it's possible to have other actions following -exec). \; will cause grep to be run once for each file while + runs a single grep on all of the files.
find . -name '*.code' -print0 | xargs -0 grep -H 'pattern'
Another common way to do this is by chaining together find and xargs. I like using -exec better, but find+xargs works just as well. The idea here is that xargs takes file names passed in on stdin and runs the named command with those file names appended. To get a suitable list of file names passed in we pipe the output of a find command into xargs.
The -print0 option tells find to print each file it finds along with a NUL character (\0). This goes hand in hand with xargs's -0 option. Using -print0 and -0 ensures that we can handle file names with unusual characters like whitespace and quotes correctly.
来源:https://stackoverflow.com/questions/15414876/using-find-and-grep-to-mimic-findstr-command