Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I understand that grep matches all the blank lines for the pattern ^$. But what does just ^ means. When I gave grep '^' filename. It matched all the lines.

Also grep '' filename matched all the lines

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

The grep man page explains both symbols:

Anchoring
The caret ^ and the dollar sign $ are meta-characters that respectively match the empty string at the beginning and end of a line.

Searching for ^ 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 (^foo), or all lines that end with bar (bar$)

share|improve this answer
add comment

^ 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

eg.

grep -Ev '^$|^#'  /etc/sudoers

It will exclude blank line and line which is start from hash ( # )

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.