Exclude dash in grep | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Exclude dash in grep

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-21-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 16 September 2014, 8:52 AM EDT
Posts: 328
Thanks: 35
Thanked 86 Times in 85 Posts
Exclude dash in grep

Hi,

I must be overlooking something, but I don't understand why this doesn't work. I'm trying to grep on a date, excluding all the lines starting with a dash:

testfile:

Code:
#2013-12-31
2013-12-31

code:

Code:
grep '^[^#]2013-12-31' testfile

I'm expecting to see just the second line '2013-12-31' but I don't get any results. grep -v is not an option btw.
Sponsored Links
    #2  
Old 01-21-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 16 September 2014, 1:24 AM EDT
Location: Amsterdam
Posts: 9,443
Thanks: 278
Thanked 2,380 Times in 2,134 Posts
[^#] means a single character that is not a hash-sign.
Why not use:

Code:
grep '^2013-12-31'

But perhaps you mean this:

Code:
grep '^[^#]*2013-12-31'

The Following User Says Thank You to Scrutinizer For This Useful Post:
Subbeh (01-21-2013)
Sponsored Links
    #3  
Old 01-21-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 September 2014, 8:08 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Elementary. [^#] will match one character which is not an octothorpe (or hash). So, what you are asking grep to match is one non-hash character (mandatory for the overall pattern to match) at the beginning of a line followed by 2013-12-31 .

Get it?
The Following User Says Thank You to elixir_sinari For This Useful Post:
Subbeh (01-21-2013)
    #4  
Old 01-21-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 16 September 2014, 8:52 AM EDT
Posts: 328
Thanks: 35
Thanked 86 Times in 85 Posts
Quote:
Originally Posted by Scrutinizer View Post
[^#] means a single character that is not a hash-sign.
Why not use:

Code:
grep '^2013-12-31'

But perhaps you mean this:

Code:
grep '^[^#]*2013-12-31'

The problem is that the string doesn't always start with the date. It could be @2013-12-31 for example.


Code:
grep '^[^#]*2013-12-31'

seems to work by the way. Thanks
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep for a srting & exclude two folders mohtashims Shell Programming and Scripting 1 12-07-2012 10:13 AM
Exclude dash (-) from word separators in vi cabhi UNIX for Advanced & Expert Users 3 03-22-2012 12:57 AM
Using Grep Include/Exclude Files metallica1973 Shell Programming and Scripting 8 10-28-2011 01:08 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 02:59 AM
grep - to exclude lines beginning with pattern frustrated1 Shell Programming and Scripting 2 08-29-2005 07:18 AM



All times are GMT -4. The time now is 09:28 AM.