Windows is similar but uses an additional Carriage Return, represented as /r, before its Line Feed. In the case of Unix-style systems (where grep originated), this is a simple Line Feed, represented as /n e.g. Importantly, these can vary from operating system to operating system. Newlines are normally hidden control characters in a text document which signal to an application where to separate lines of text. You can read more about GNU sed regular expressions here. A missing final / will result in an "unterminated 's' command" error.Īs a small caveat, the sed command above eliminates sub-directory information as well, so the file names returned may not all be in the same (root) directory.+ in case of conflicts (say, with directory slashes). // is, respectively, the required / separator between our item to find and our replacement, no space (a blank replacement) and the final / required for all sed substitute commands.\+ is a regular expression (what we want to find - in this case everything up to and including the last / in our grep output). s/ is the beginning of the substitute command.The breakdown of the sed command is as follows: This will leave just the file name in "results.txt" and has the added benefit of eliminating any issues with / and Unix-style line feeds (all / are removed and, at least when piping like this, sed outputs normal Windows line breaks). ![]() In our case with GNUWin32 grep, we can use it to eliminate everything before each file name with: sed s/.\+//įurthermore, we can pipe | the output from grep to sed like so: grep -rilf C:\path\to\searchwords.txt C:\path\to\search | sed s/.\+// > results.txt What this means is that this program can take text input from the command line, modify it in a standardized way, and output that modified data. This is a port of GNU sed, a "non-interactive command-line text editor". ![]() Since you want just the file names, we can actually use another GNUWin32 utility called sed to obtain these. Note that there is some additional explanation of these last two GNUWin32 grep specific quirks at end of this answer. It's also worth mentioning that the reason I have placed the "searchwords.txt" file in a separate directory is that GNUWin32 grep will include this file in the "matched" items otherwise.įinally, while it shouldn't be a deal-breaker in most instances, GNUWin32 grep uses Unix-style line feeds for its output, as well as a mix of \ and / for paths. doc1.txt doc2.txt doc3.txt) in place of the directory. If you wish to search only specific files, you can list them on the command line individually (e.g. Any paths with spaces require double quotes ("") as normal. For instance, -L is not the same option as -l (lowercase L). Be aware, however, that unlike FINDSTR, grep options are typically case-sensitive. Suppress normal output (list files only, including full path).īoth FINDSTR and grep return full paths to a file, so you will likely need to do additional processing on the output of either. For your use case, you can try: grep -rilf C:\path\to\searchwords.txt C:\path\to\search > results.txt While grep implementations can vary in the options they support, GNUWin32 grep is a port of GNU grep (albeit currently v2.5.4 rather than v3.0) which can read search terms from a file. Note, however, FINDSTR has undocumented limits and bugs which may not make it a suitable option.įor a non-native solution, you might be interested in a port of the grep utility, such as GNUWin32 grep. Print only file names (which happens to include the full directory path). Use the search words as string literals (rather than regular expressions). ![]() For your use case, you could try: findstr /l /s /i /m /g:searchwords.txt /f:filestosearch.txt > results.txt For a native Windows solution, FINDSTR is a possible option.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |