...
For basic command-line pattern matching, I first try grep. Even though there are several grep utilities (grep, egrep, fgrep), I tend to use the original grep command. While by default it implements clunky POSIX-style pattern matching, its -P argument asks it to honor perl regular expression syntax. In most cases this works well.
...
- -v (inverse) – only print lines with no match
- -n (line number) – prefix output with the line number of the match
- -i (case insensitive) – ignore case when matching alphanumeric characters
- -c (count) – just return a count of the matches-n (line number) – prefix output with the line number of the match
- -L – instead of reporting each match, report only the name of files in which a match is found
- handy for checking a bunch of log files for errors or success
- -A (After) and -B (Before) – output the specified number of lines before and after a match
...
If grep isn't behaving the way I expect, I turn to perl. Here's how to invoke regex pattern matching from a command line using perl:
Code Block | ||
---|---|---|
| ||
perl -n -e 'print if $_=~/<some pattern>/;' # for example: echo -e "12\n23\n4\n5" | perl -n -e 'print if $_ =~/\d\d/' |
...