I understand that grep matches all the blank lines for the pattern
^$. But what does just
^ mean? When I gave
grep '^' filename, it matched all the lines.
grep '' filename matched all the lines.
grep man page explains both symbols:
^and the dollar sign
$are meta-characters that respectively match the empty string at the beginning and end of a line.
^ just matches the beginning of the line, which every line has, so they all match. Searching for an empty string has no constraints at all, so it also matches all lines. Searching for
^$ means "match the beginning of the line followed by the end of the line", which is a blank line. You can also use them for cases like finding all lines that start with
^foo), or all lines that end with
^ is for the beginning of a line.
^$ is saying beginning of a line, end of a line
$, with nothing in between.
using this regex you can find blank lines and delete/exclude blank line
grep -Ev '^$|^#' /etc/sudoers
It will exclude blank line and line which is start from hash ( # )