Grep character classes '\w' '\d'


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Grep character classes '\w' '\d'
# 1  
Old 02-27-2010
Grep character classes '\w' '\d'

I am learning regex fundamentals on my own and when I try to use \w for characters (i.e. [a-zA-Z]), or \d for digits ([0-9]) it doesnt work even though I see in greps man page that \w should be the same as [[:alphanum:]]...

[[:alphanum:]] and those types of syntactic character classes do work for me, its just the shorthand \w \W and \d \D dont. I am using mac os x which should be similar to freebsd with bash shell.

?

---------- Post updated at 02:36 PM ---------- Previous update was at 02:32 PM ----------

I meant [[:alnum:]] not alphanum. Where can I get a list of character classes that work besides he man page? I am using: grep (GNU grep) 2.5.1

---------- Post updated at 03:30 PM ---------- Previous update was at 02:36 PM ----------

I am becoming increasingly confused about regex and bash.. the regular expressions that work with vi differ from the ones that work with bash? can someone break it down to me where pattern matching and regex differ?
# 2  
Old 02-27-2010
There are multiple variants of regular expression. There are the original POSIX basic regex, with a very simple syntax. Then there's the POSIX extended regex, which is basically basic POSIX regex with a simpler usage. Both support character classes like [:alnum:], [:blank:] and the like, as a means of saving characters & improving readability.

A lot more powerful are Perl Compatible Regular Expressions (PCRE), as copied by almost any modern regex engine (Java, Ruby, PHP, .Net, ...). That introduced backslash sequences as a means for writing character classes even shorter, eg \d for any single digit, \W for any non-word character, ...

Now you're saying that the regex don't work for you. Could you give an example of what's your input, and what command line you've tried? Maybe the issue isn't with the regex, but the way it's used.
# 3  
Old 02-27-2010
Well is seems you're saying that \w is a post grep regex character class, and used for PERL but not grep? I would try something like
Code:
#echo hello |grep \w+

and get no results.

---------- Post updated at 04:44 PM ---------- Previous update was at 04:38 PM ----------

Code:
 echo hello |grep [[:lower:]]
hello


echo hello |grep [a-z]
hello

These work. I expected
Code:
echo 123 |grep \d

to give me 123 back

I guess \w \W \d \D dont work with grep, they dont seem to work in vim either.
# 4  
Old 02-27-2010
I just went through the man page for grep, and it seems that grep, in it's basic incarnation, does not support the sequences introduced by Perl. That is, until you add the -P switch:
Quote:
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression. This is highly experimental and grep -P may warn of unimplemented features.
Code:
$ echo '123' | grep '\d'
$ echo '123' | grep -P '\d'
123

And no, vim usually isn't compiled with support for PCREs, at least on no system I've ever check for it.
# 5  
Old 02-28-2010
Wow, even after going back and looking for it I couldnt find the -p switch, I actually had to grep the grep manual for it! Thanks, that's what I was looking for!

---------- Post updated at 09:37 PM ---------- Previous update was at 12:53 AM ----------

I got some errors using the -P switch, but I found the \w character class works well when double quoted and with the -E switch for extended regex.

---------- Post updated at 09:40 PM ---------- Previous update was at 09:37 PM ----------

Code:
:~ localadmin$ echo "Hello 123 abc <># .. asdf" |grep -Eo "\w+"
Hello
123
abc
asdf


STLM011:~ localadmin$ echo "Hello 123 abc <># .. asdf" |grep -o "\w+"
STLM011:~ localadmin$ 

STLM011:~ localadmin$ echo "Hello 123 abc <># .. asdf" |grep -oE \w+
STLM011:~ localadmin$ 

STLM011:~ localadmin$ echo "Hello 123 abc <># .. asdf" |grep -oE "\W+"
 
 
 <># .. 
STLM011:~ localadmin$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep only some part of character?

Hi, I have log : # cat log $ cat 4 2014092014 2014102014 2014112023 2014123014 2014010100 2014010101 2014010102 2014010103 2014010104 2014020123 2014020115 2014020116 (3 Replies)
Discussion started by: justbow
3 Replies

2. Shell Programming and Scripting

Grep -F for special character

a='CASH$$A' /usr/xpg4/bin/grep -F "$a" *.txt It is not able to grep CASH$$A string as it contains special character $$. I also tried with /usr/xpg4/bin/grep -F '$a' *.txt but still not working. I have to assign CASH$$A to a variable and serach that variable..i dont want to search the... (8 Replies)
Discussion started by: millan
8 Replies

3. UNIX for Dummies Questions & Answers

Grep character æ

Hello, Im trying check if character "æ" (alt+145) is included or not in some files. To do that I have written a scrip with vi that basically is a loop of all these compressed files and something like: zcat $file | grep æ >> logtocheck.out ; The problem is that æ is translated to something like... (3 Replies)
Discussion started by: ngb
3 Replies

4. UNIX for Dummies Questions & Answers

How to grep until a certain character?

Hi, so I have a file containing many repetitions of the pattern block displayed below: >NM_13489075 ACGUGCUAGCUUAGCGA AGCUAGCUGAUCGAUGC ACGUAGCUAGCUGAUCG GAUCGA >NM_13489023 ACGUGCUAGCUUAGCGA AGCUAGCUGAUCGAUGC ACGUAGCUAGCUGAUCG GAAGUC I was wondering how to grep, for example, a... (5 Replies)
Discussion started by: ShiGua
5 Replies

5. Programming

c++ help with class(new to classes)

Hello there, I am new to using classes, and have been having so many problems. I don't want to go to my teacher if I don't have to, because it is always my luck that it is something easy that I just overlooked somehow. I have been working on this for 3 days and I can't get it to read from a file. ... (1 Reply)
Discussion started by: KingAroan
1 Replies

6. Shell Programming and Scripting

grep ^ as a character

I have a pwd file with a number of errors like: ^grep ^ I get the whole file since it thinks it's new line. Should I \ to escape this? (4 Replies)
Discussion started by: dba_frog
4 Replies

7. Programming

Use of C++ Classes

I was wondering if I could put the section at the beginning rather than at the end before the definition of the class. const REAL ModMisfit::DefMinDT = 0.01; const REAL ModMisfit::DefSigma0 = 0.01; const double ModMisfit::DefDAngSh = 2; const REAL ModMisfit::DefKBeta = 5;... (2 Replies)
Discussion started by: kristinu
2 Replies

8. UNIX for Advanced & Expert Users

grep in special character

All, I am trying to grep "-----" from a test when i use this i am getting the below error. What is the reason for this ?????... How can i over come this ##) echo "----------------- test_sys_job -----------------" | grep "-----------------" grep: illegal option -- - grep: illegal... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

9. Programming

how to use classes in c ?!?!?

Hi, I've tried to use classes in my program, but the compiler simply gives an error on the word class . Am I the only one with this problem ? I have no idea how to use classes in c in linux environment(suse). If you've got any idea what should I do I would be very thankful. Thanks to ya all !... (4 Replies)
Discussion started by: atticus
4 Replies

10. UNIX for Dummies Questions & Answers

Grep for X character on a word

How can I grep for a certain letter that only shows on the 3rd letter or character. ex: ASGHDY SHTYRD SDTYRD IGIKGD I only want the TY part of the 3rd character so output would only be SHTYRD and DDTYRD - I only want the TY on the 3rd character. THANKS (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question