output of grep

hi,

why the following is giving entire /etc/passwd  file as output
 even when it does not contain ^ or $  .

   grep  ' [^$] '   /etc/passwd


    whereas the following is not giving any output


       grep ' ^$ ' /etc/passwd

I suggest you read the more about regular expressions. If you do not have any material, then read man 7 regex. It explains about the meaning of ^, $, [] et al.

Hey,

When expression /^$/ comes to sed or awk it means the blank line.

But when i checked it with grep it perfectly shows the o/p.
i had
---------------
cc
dsdsd

dsddddsdfgfgfgfgfgf

---------------
and when i used the grep as..
grep '[^$]' x.txt
it shows ..
-------------------
cc
dsdsd
dsddddsdfgfgfgfgfgf
-------------------

Its working !!:b:

grep ' [^$] ' /etc/passwd

It is a bit tricky question :), So first read it carefully :slight_smile:

^ Represents the beginning of the line,
sanjay ~> grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash

$ Represents the end of the line.
sanjay ~> grep :blush: /etc/passwd
news:x:9:13:news:/var/spool/news:

[ ] (Brackets) = match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a range, as in [0-9].
[^ ] = match any one character except those enclosed in [ ], as in [^0-9].

grep '^$' files {search for blank lines}

Now you got what it for searching if you will givegrep ' [^$] ' /etc/passwd