nerdexam
LPI

010-100 · Question #61

Given a file called birthdays containing lines like: YYYY-MM-DD Name 1983-06-02 Tim 1995-12-17 Sue Which command would you use to output the lines belonging to all people listed whose birthday is in…

The correct answer is C. grep '[0-9]*-0[56]-' birthdays. This question tests the ability to write a grep regular expression that matches month fields 05 (May) and 06 (June) in a YYYY-MM-DD date format.

The Power of the Command Line

Question

Given a file called birthdays containing lines like:

YYYY-MM-DD Name 1983-06-02 Tim 1995-12-17 Sue Which command would you use to output the lines belonging to all people listed whose birthday is in May or June?

Options

  • Agrep '[56]' birthdays
  • Bgrep 05?6? birthdays
  • Cgrep '[0-9]*-0[56]-' birthdays
  • Dgrep 06 birthdays | grep 05

How the community answered

(38 responses)
  • A
    8% (3)
  • B
    5% (2)
  • C
    84% (32)
  • D
    3% (1)

Why each option

This question tests the ability to write a grep regular expression that matches month fields 05 (May) and 06 (June) in a YYYY-MM-DD date format.

Agrep '[56]' birthdays

The pattern '[56]' matches any line containing a 5 or 6 anywhere, including year digits or day digits, producing many false positives unrelated to May or June.

Bgrep 05?6? birthdays

In basic regular expressions, '?' makes the preceding character optional, so '05?6?' matches strings like '0', '05', '06', or '056' but does not reliably isolate the month field as 05 or 06.

Cgrep '[0-9]*-0[56]-' birthdaysCorrect

The pattern '[0-9]*-0[56]-' anchors the match to the month position by requiring a literal '-0' before the character class '[56]', which matches either 5 or 6, followed by another '-'. This precisely targets months 05 and 06 in the date field without matching digits elsewhere in the line.

Dgrep 06 birthdays | grep 05

Piping 'grep 06' into 'grep 05' requires both substrings to appear on the same line simultaneously, which is impossible for a single date value and would match nothing.

Concept tested: grep regular expressions for date field pattern matching

Source: https://www.gnu.org/software/grep/manual/grep.html

Topics

#grep#regex#pattern matching#text search

Community Discussion

No community discussion yet for this question.

Full 010-100 Practice