Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Exclude dash in grep

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-21-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 7 May 2015, 3:00 AM EDT
Posts: 332
Thanks: 37
Thanked 87 Times in 86 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 Unix and Linux 01-21-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 29 August 2015, 4:12 AM EDT
Location: Amsterdam
Posts: 10,423
Thanks: 358
Thanked 2,765 Times in 2,471 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 Unix and Linux 01-21-2013
elixir_sinari's Unix or Linux Image
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 16 March 2015, 7:53 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 Unix and Linux 01-21-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 7 May 2015, 3:00 AM EDT
Posts: 332
Thanks: 37
Thanked 87 Times in 86 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 Linux
Sponsored Links
Closed Linux or Unix Question

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
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 08:38 AM.