What the equivalent of "grep | cut" using sed or awk?

How about using awk?

awk -F = '/email2/ { print $2}' /etc/emails.conf
  • -F = Fields are separated by '='

  • '/email2/ { print $2}' On lines that match "email2", print the second field


The exact equivalent would be something like:

sed -n '/email2/{s/^[^=]*=\([^=]*\).*/\1/;p;}' < file

But you'd probably want instead:

sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file

(that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the second = if any).


perl -nlE 's/email2\s*=\s*// and say'    file

Where:

  • perl -nl is a for each line do...
  • s/email2 = // removes the searched email id and if you could do it ...
  • say prints the current input input line
  • \s* zero or more spaces (equivalent to [ \t\n]*)

Tags:

Grep

Awk

Sed

Cut

Cat