...
- grep -P '<pattern>' searches for <pattern> in its input, and only outputs lines containing it
- always enclose '<pattern>' in single quotes to inhibit shell evaluation!
- pattern-matching metacharacters are very different from those in the shell
- -P says to use Perl patterns, which are much more powerful than standard grep patterns
- -c says just return a count of line matchesv (inverse match) – only print lines with no match
- -n says include the (line number) – prefix output with the line number of the matching linematch
- -v (inverse match) says return only lines not matching the pattern-l i (case insensitive) – ignore case when matching
- -l says return only the names of files that do contain the pattern match
- -L says return only the names of files that do not contain the pattern match
- -Lc says return only the names of files containing no pattern matchesjust return a count of line matches
- -A <n> (After) and -B <n> (Before) – output '<n>' number of lines after or before a match
- always enclose '<pattern>' in single quotes to inhibit shell evaluation!
A regular expression (regex) is a pattern of characters to search for and metacharacters that control and modify how matching is done.
...
- here's a good general one: https://www.regular-expressions.info/
- and a perl regex tutorial: http://perldoc.perl.org/perlretut.html
- perl regular expressions are the "gold standard" used in most other languages
Field delimiter summary
Be aware of the default field delimiter for the various bash utilities, and how to change them:
utility | default delimiter | how to change | example |
---|---|---|---|
cut | Tab | -d or --delimiter option | cut -d ':' -f 1 /etc/passwd |
sort | whitespace (one ore more spaces or Tabs) | -t or --field-separator option | sort -t ':' -k1,1 /etc/passwd |
awk | whitespace (one ore more spaces or Tabs) Note: some older versions of awk do not treat Tabs as field separators. |
|
cat /etc/passwd | awk -F ":" '{print $1}' |
join | one or more spaces | -t option |
|
perl | whitespace (one ore more spaces or Tabs) when auto-splitting input with -a | -F'/<pattern>/' option | cat sampleinfo.txt | perl -F'/\t/' -a -n -e 'print "$F[0]\t$F[2]\n";' |
read | whitespace (one or more spaces or Tabs) | IFS= (input field separator) option | Note that a bare IFS= removes any field separator, so whole lines are read each loop iteration. |