



This solution will slow with larger interval sizes, whereas don's will slow with larger interval frequencies. It works by building a look-ahead buffer of $B-count lines before ever attempting to print anything.Īnd actually, probably I should clarify my previous point: the primary performance limiter for both this solution and don's will be directly related to interval. This is an example of what is called a sliding window on input. <(grep PATTERN -A1 -B2 <(nl -ba -nrz -s: infile) | sort) | cut -d: -f2-ĭon's might be better in most cases, but just in case the file is really big, and you can't get sed to handle a script file that large (which can happen at around 5000+ lines of script), here it is with plain sed: sed -ne:t -e"/\n.*$match/D" \ With join: join -t: -j1 -v1 <(nl -ba -nrz -s: infile | sort) \ <(nl -ba -nrz -s: infile | sort) | cut -d: -f2-Ĭomm requires sorted input which means the line order would not be preserved in the final output (unless your file is already sorted) so nl is used to number the lines before sorting, comm -13 prints only lines unique to 2nd FILE and then cut removes the part that was added by nl (that is, the first field and the delimiter :) With comm: comm -13 <(grep PATTERN -A1 -B2 <(nl -ba -nrz -s: infile) | sort) \ Other ways that don't preserve line order and are most likely slower: though if the input has only a few matches it's not worth doing it. I think this could be slightly optimized if it collapsed any three or more consecutive line numbers into ranges so as to have e.g. This should also work with files of patterns passed to grep via -f e.g.: grep -n -A1 -B2 -f patterns infile | \ The following wildcard characters can be used for the name pattern definition.You could use gnu grep with -A and -B to print exactly the parts of the file you want to exclude but add the -n switch to also print the line numbers and then format the output and pass it as a command script to sed to delete those lines: grep -n -A1 -B2 PATTERN infile | \ $ grep -R "script" -exclude-dir= Exclude Directories with Name Patternĭirectories can be also excluded according to their names by using name patterns. The directory names are provided inside the curly brackets like below. $ grep -R "script" -exclude-dir=backup -exclude-dir=abcĪlternatively, we can use the single –exclude-dir option in order to exclude multiple directories. The -exclude-dir can be used multiple times in order to define multiple directories to exclude from a grep match. We may need to exclude multiple directories for a single grep search. $ grep -R "script" -exclude-dir=backup Exclude Multiple Directories In the following example, we exclude the directory named backup. The -exclude-dir option is used to specify the directory we want to exclude for the grep match. Exclude DirectoryĪ directory can be excluded from the grep command search. Even though it is a good feature in some cases we may need to exclude some directories for the grep command. One of the powerful features of the grep command is the ability to search multiple directories recursively.

The Linux grep command is used to search and filter files and folders for the specified search term or regex pattern.
