Then output the count to each pattern to either standard output or count.txt. Terminal displays only the final results of the two commands combined. grep -E -r Pattern 1 data/ wc -l grep -E -r Pattern 2 data/ wc -l grep -E -r Pattern 3 data/ wc -l Now Im trying to put this all into a shell script with one argument, which would be parent directory of data/. The first part of the command looks for the word Walden in any files in the current directory, and the second runs another grep command on the results of the first command. You’d use this command: grep Walden * | grep Pond. Say you want to find files containing both Walden and Pondon the same line. To search recursively across all files (across multiple lines within each file) with EITHER string present (i.e. Using the pipe ( |), a Unix redirection operator, you can tell grep to search for more than one string. (Note that you can also combine options-for instance, grep -rl Walden searches subfolders and returns only a list of files containing the word Walden. But is it possible to so domething like: grep -c -e alfa -e beta -e gamma -somemoreblackmagic file. I can do: grep -c alfa file 1 grep -c beta file 1 grep -c gamma file 2. Get started with the helpful options listed here. Is it possible to do a grep count of multiple occurrences in a file in one single command For example: cat > file blah alfa beta blah blah blahgamma gamma. Assuming all your strings to check are in a file strings.txt, and the file you want to check in is input.txt, the following one liner will do : Updated the answer based on comments : diff <( sort -u strings.txt ) <( grep -o -f strings.txt input.txt sort -u ) Explanation : Use grep's -o option to match only the strings you are interested in. The grep command has several options that let you fine-tune the way you search for text, as well as the kind of results grep returns. Returns the names of files containing Walden and the number of hits in each file. Finds Walden in any file in any subfolder of ~/Documents.įinds only live does not find liver, lives, lived, and so on.įinds files containing Walden, but returns only a list of file names.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |