Confusion in working of carat(^) symbol

hi All,

i have googled and found that below are the functionality of carat symbol
Meaning of carat is

"at the beginning of line"; 
"it can also negate matches"

how do i know when carat is behaving as beginning of line or as a negate.

regards,
scriptor

From the context. First we need to know that you seem to be talking of regexes, because a carat can have even more meanings, e.g. exponentiation in several programming languages, upper case conversion in certain shells' "Case Modification Parameter Expansion", or used to produce characters like in the French locale.
In regexes, if used as the first character in a matching pattern, it anchors the pattern at BOL. If not the first character, it is treated as is, i.e. there must be a carat in the text to match the carat in the regex.
In a bracket expression, if the list begins with '^', it matches any single character not from the rest of the list.

Hi Rudic,
didn't understand below line

 
 In a bracket expression, if the list begins with '^', it matches any single character not from the rest of the list.
 

I create a file

 
 cat test.txt 
no color
No color
 

I do the following

 
 # grep ^[^nN] test.txt  ---> this gives no output why so ?
 
 
 # grep ^[nN] test.txt-------> this gives o/p
no color
No color

 

In a [character set] if the first character is ^ it means NOT the following character set.
^[^nN] means at the beginning of the line there must be a character that is not n or N.
BTW please always put grep regular expression in "quotes" or 'quotes'. Then it is guaranteed that the shell does not try to make any expansion but passes the string to grep as is.

grep "^[^nN]" test.txt

One thing to also note is that

grep "^[^nN]"

also removes empty lines (since it does not start with a non-n or N character), whereas

grep -v "^[nN]"

does not remove empty lines

HI MadeInGermany

when you say

In a [character set] if the first character is ^ it means NOT the following character set.

then the below command should not give O/P but it is giving. by say not the following character I mean

nN

.
so ideally it should not give any output.

 
  
 grep ^[nN] test.txt
 
grep "^[nN]" test.txt

matches at the beginning of the line an n or N

no color
No color

Both lines match.