Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • 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 (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

A regular expression (regex) is a pattern of characters to search for and metacharacters that control and modify how matching is done.

...

Field delimiter summary

Be aware of the default field delimiter for the various bash utilities, and how to change them:

utilitydefault delimiterhow to changeexample
cutTab-d or --delimiter optioncut -d ':' -f 1 /etc/passwd
sortwhitespace
(one ore more spaces or Tabs)
-t or --field-separator optionsort -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.

  • In the BEGIN {  } block
    • FS= (input field separator)
    • OFS= (output field separator)
  • -F or --field-separator option

cat sampleinfo.txt | awk 'BEGIN{ FS=OFS="\t" } {print $1,$3}'

cat /etc/passwd | awk -F ":" '{print $1}'
joinone or more spaces-t option
join -t $'\t' -j 2 file1 file12
perlwhitespace
(one ore more spaces or Tabs)
when auto-splitting input with -a
-F'/<pattern>/' optioncat sampleinfo.txt | perl -F'/\t/' -a -n -e 'print "$F[0]\t$F[2]\n";'
readwhitespace
(one or more spaces or Tabs)
IFS= (input field separator) optionNote that a bare IFS= removes any field separator, so whole lines are read each loop iteration.