Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Exclude dash in grep

Shell Programming and Scripting


Closed    
 
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: 3 March 2016, 7:23 AM EST
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: 2 December 2016, 2:51 AM EST
Location: Amsterdam
Posts: 11,096
Thanks: 440
Thanked 3,105 Times in 2,747 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: 29 November 2016, 6:07 AM EST
Location: India
Posts: 1,413
Thanks: 101
Thanked 497 Times in 474 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: 3 March 2016, 7:23 AM EST
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

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 11:13 AM
Exclude dash (-) from word separators in vi cabhi UNIX for Advanced & Expert Users 3 03-22-2012 01:57 AM
Using Grep Include/Exclude Files metallica1973 Shell Programming and Scripting 8 10-28-2011 02:08 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 03:59 AM
grep - to exclude lines beginning with pattern frustrated1 Shell Programming and Scripting 2 08-29-2005 08:18 AM



All times are GMT -4. The time now is 03:51 PM.