Grep command functionality - discussed

what is the following bash command does

grep `date +%Y-%m-%d --date='1 day ago'` /path/to/file/FILE_PREFIX_\`date +%Y%m%d --date='1 day ago'`.dsv | grep -v 'ERROR' | cut -d "|" -f 2 | sed 's/^0/27/'

I'm afraid the title you selected is more than misleading. We're not discussing grep but shell functionality (see below).

There are two steps you can do to elicit how it works from a (compound , complex) command line:

  • set the -x ( --xtrace ) option (or its equivalent) of your shell and execute again. The shell will unveil a lot (not all, alas) of what it does with the parts of the command(s).
  • isolate single commands and execute by themselves, like
$ date +%Y-%m-%d --date='1 day ago'
2019-02-23
$ date +%Y%m%d --date='1 day ago'
20190223

So this would result in a command

grep 2019-02-23 /path/to/file/FILE_PREFIX_\20190223.dsv

, from which output the lines containing "ERROR" are removed by an "inverted match" when piped through grep -v . Then, the second | delimited field is cut out, and in those, any single leading zero is s ubstituted in the final sed .

Be aware that
a) the "backtick command substitution" `...` is deprecated and outdated and should be replaced by the modern form $(...) -
b) the parameters (not only) to the grep command should be double quoted to avoid problems should the "command substitution" 's result contain spaces et al.

1 Like