Scanning all C++ files under current directory awk -v wordsListFile=nameSpacesListFile.txt -f script.awk $(find. WordsListCount = split(wordsListStr, wordsListArr, "\n") # split wordsListStr by newLine into array wordsListArr, saved array length into wordsListCountįor (currWord in wordsListArr) wordsMatchArr = 0 # reset array wordsMatchArr to 0 Getline wordsListStr < wordsListFile # read wordsListFile as single string wordsListStr script.awk BEGIN # set record seperator to something unlikely matched, causing each file to be read entirely as a single record Print file name only if all words matched. Scanning each file once for all the words (read each file as a single record). I suggest to use gawk (standard Linux awk) script. Files pass the filter if they contain at least one instance of each word. Surely there is a way to do this? This is essentially a filtering problem: Take all the files found (recursively) inside a directory, and apply a filter to them for each of the words in the input list. Do this recursively to obtain all results in all files in a directory.Return the list of files found where ALL words in the list of search words are found in the same file.Here is what I want a grep-like tool to do: The actual name of the class I am searching for is "Parameter", which is such a generic word that it too appears in hundreds of files.In my case, the namespace "MYNAMESPACE" appears in hundreds of source files.Assume that although M圜lass and MYNAMESPACE appear to be likely to be unique strings, in general they might not be.Search for all files which contain matches for "M圜lass" in the namespace "MYNAMESPACE".I am working with some C++ source code, and I want to be able to grep for objects in my code to find the files containing the relevant information. This is a frequent problem when working with source code, so I am pretty sure there must be an adequete solution. However I am hoping that if this is the case there might be some other Linux/Unix command line tool which will do the job I want. ² also beware of characters whose encoding ends in the same encoding as that of ` or \ as is the case of several characters in some locales.I am fairly confident this can't be done with grep, unless there are some features that I don't know about. ¹ In csh, where tilde expansion is from, ~'/' would expand to the home directory of the user called / (or report a Unknown user: /. will report an error if there's no matching file.may hit the system's limit of the length of a command if there's a large number of matching files (can be worked around with zargs).works even for filenames not made of valid text.excludes hidden ones (can be added back with the D glob qualifier).Where the finding of regular files is done via recursive globbing and glob qualifiers. With the zsh shell, you can also do: grep -n GUNARS /dev/null ~/Opstk/bin/gadin-1.0/**/*.rc(.) Some grep implementations have a -H option to force the file path to be printed. When passed only one file, it doesn't print its path. The /dev/null it to make sure grep is passed at least two files. rc in or below the $HOME/Opstk/bin/gadin-1.0/ directory would be: find ~/Opstk/bin/gadin-1.0/ -name '*.rc' -type f \ Not all grep implementations support those and those that do (many for -r, few for -include) interpret them differently.Ī standard and more portable way to report lines containing GUNARS along with the file name and line number in regular files whose name ends in. In Bourne-like shells such as zsh or bash mentioned above, you could use double quotes instead of single quotes, but note that inside double quotes, there are still a few characters that retain their special meaning ( $, `, \²).įor grep, -r and -include are non-standard options. You could quote everything else if you wanted to, except that in several shells including bash, a ~ at the start of a word needs to be followed by an unquoted / or : to be expanded¹: 'grep' '-rn' '-include=*.rc' 'GUNARS' ~/'Opstk/bin/gadin-1.0/' They are the ones that must not be quoted. ~ which is expanded to the value of the HOME environment variable (your home directory) in csh-like and Korn/POSIX-like shells and a few more modern shells at least.space that is used here to separate arguments.The characters that are special in the syntax of the shell in that code are: The GUNARS doesn't need to be quoted as none of those 6 characters are special in the syntax of the shell. Or: grep -rn -include='*'.rc GUNARS ~/Opstk/bin/gadin-1.0/ echo REPLACEDnsearchFor2 with replaceWith2 recursively through directory listed belownFor all files that current user has write permissions for. Here, you want to pass a literal -include=*.rc to grep, so: grep -rn '-include=*.rc' GUNARS ~/Opstk/bin/gadin-1.0/ Zsh% : > -include=foo.rc > -include=bar.rc See for instance: zsh% echo -include=*.rc rc using a shell feature called globbing, filename generation or pathname expansion. include=*.rc is the way to ask the shell for the list of file names in the current directory that start with -include= and end in.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |