Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Confusion in working of carat(^) symbol

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-21-2017   -   Original Discussion by scriptor
scriptor's Unix or Linux Image
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 9 April 2018, 9:00 AM EDT
Posts: 180
Thanks: 21
Thanked 0 Times in 0 Posts
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


Code:
"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
Sponsored Links
    #2  
Old Unix and Linux 12-21-2017   -   Original Discussion by scriptor
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 April 2018, 12:03 PM EDT
Location: Aachen, Germany
Posts: 12,497
Thanks: 401
Thanked 3,872 Times in 3,560 Posts
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.

Last edited by RudiC; 12-21-2017 at 06:42 AM..
Sponsored Links
    #3  
Old Unix and Linux 12-21-2017   -   Original Discussion by scriptor
scriptor's Unix or Linux Image
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 9 April 2018, 9:00 AM EDT
Posts: 180
Thanks: 21
Thanked 0 Times in 0 Posts
Hi Rudic,
didn't understand below line


Code:
 
 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



Code:
 
 cat test.txt 
no color
No color

I do the following


Code:
 
 # grep ^[^nN] test.txt  ---> this gives no output why so ?



Code:
 
 # grep ^[nN] test.txt-------> this gives o/p
no color
No color

    #4  
Old Unix and Linux 12-21-2017   -   Original Discussion by scriptor
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 20 April 2018, 12:22 PM EDT
Location: Simplicity
Posts: 4,034
Thanks: 346
Thanked 1,356 Times in 1,222 Posts
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.


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

Sponsored Links
    #5  
Old Unix and Linux 12-21-2017   -   Original Discussion by scriptor
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 April 2018, 5:13 PM EDT
Location: Amsterdam
Posts: 11,781
Thanks: 542
Thanked 3,430 Times in 3,024 Posts
One thing to also note is that


Code:
grep "^[^nN]"

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


Code:
grep -v "^[nN]"

does not remove empty lines
Sponsored Links
    #6  
Old Unix and Linux 12-22-2017   -   Original Discussion by scriptor
scriptor's Unix or Linux Image
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 9 April 2018, 9:00 AM EDT
Posts: 180
Thanks: 21
Thanked 0 Times in 0 Posts
HI MadeInGermany

when you say


Code:
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

Code:
nN

.
so ideally it should not give any output.


Code:
 
  
 grep ^[nN] test.txt

Sponsored Links
    #7  
Old Unix and Linux 12-22-2017   -   Original Discussion by scriptor
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 20 April 2018, 12:22 PM EDT
Location: Simplicity
Posts: 4,034
Thanks: 346
Thanked 1,356 Times in 1,222 Posts


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

matches at the beginning of the line an n or N


Code:
no color
No color

Both lines match.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Confusion with PS sathyaonnuix Shell Programming and Scripting 11 09-28-2012 06:01 AM
Stripping of a symbol from string not working properly angie1234 Shell Programming and Scripting 13 03-29-2012 12:10 PM
/usr/lib/passwdutil.so.1: symbol __nsl_fgetspent_r: referenced symbol not found dshakey Solaris 0 03-18-2010 02:11 PM
'tr' confusion Nafanja UNIX for Dummies Questions & Answers 2 12-05-2007 10:47 PM
Invoke URL's from file and comment with # symbol which are not working gsp UNIX for Dummies Questions & Answers 0 08-23-2007 02:55 AM



All times are GMT -4. The time now is 12:36 PM.