LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with grep and regex (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-grep-and-regex-4175603534/)

MBoyle19 04-09-2017 08:34 PM

Need help with grep and regex
 
echo "The fox jumped over 3 fences" | grep '[0-9]'
The fox jumped over 3 fences

But , using the regex \d produce a different result

echo "The fox jumped over 3 fences" | grep '\d'
The fox jumped over 3 fences

Why do these types of regex \d \w \s \w do not work with grep or am I using it wrong. Does sed and awk understand those regex types above? Thank you in advance!

syg00 04-09-2017 08:51 PM

There are several variations in regex - regex ain't regex.

-E gets you extended regex, -P gets you PCRE.

MBoyle19 04-09-2017 09:25 PM

Quote:

Originally Posted by syg00 (Post 5694943)
There are several variations in regex - regex ain't regex.

-E gets you extended regex, -P gets you PCRE.

????? :scratch:

syg00 04-09-2017 09:36 PM

Code:

man grep
man 7 regex
man perlre


frankbell 04-09-2017 09:44 PM

Dave Morris has done some podcasts about awk and sed at Hacker Public Radio; the shownotes are extremely detailed. They might be a help, as regex is integral to expertise in sed and awk.

(If I ever understand regex, I will feel entitled to call myself a "Linux geek" instead of a "Linux guy.":))

TobyV 04-09-2017 09:57 PM

Quote:

Originally Posted by MBoyle19 (Post 5694954)
????? :scratch:

I believe syg00 is telling you to use grep with the -P option for grep to understand perl's short hand regular expressions.

Code:

grep -P '\d'

MBoyle19 04-10-2017 12:12 AM

I have one more problem.

Code:

a="abc [ 123 ]"

echo "${a//[^a-zA-Z0-9]/ }"

abc  123

How to suppress the extra white space?

pan64 04-10-2017 12:21 AM

would be nice to show us what is the expected result and/or more examples. And how do you want to solve it (with perl/sed/awk/bash or ???)

MBoyle19 04-10-2017 12:24 AM

I want it to look like this abc 123 using bash

pan64 04-10-2017 12:28 AM

Code:

echo ${a//[^a-zA-Z0-9]/ }
(no " )


MBoyle19 04-10-2017 12:40 AM

Quote:

Originally Posted by pan64 (Post 5694992)
Code:

echo ${a//[^a-zA-Z0-9]/ }
(no " )


yes it works! Thanks you pan64!

I thought you had to use quotes if a variable had spaces.

Sefyir 04-10-2017 12:45 AM

Quote:

Why do these types of regex \d \w \s \w do not work with grep or am I using it wrong.
Having just gone over them, these remind me of the shorthand regexs that python uses

MBoyle19 04-10-2017 12:53 AM

Quote:

Originally Posted by Sefyir (Post 5694994)
Having just gone over them, these remind me of the shorthand regexs that python uses

I didn't know the exact name they were called. So I called it regex types. Tobyv did mentioned above it was called short hand regex.

Can sed and awk understand shorthand regex?

pan64 04-10-2017 01:09 AM

Quote:

Originally Posted by MBoyle19 (Post 5694993)
yes it works! Thanks you pan64!

glad to help you
if you really want to say thanks just click on yes
Quote:

Originally Posted by MBoyle19 (Post 5694993)
I thought you had to use quotes if a variable had spaces.

And this is the case when you do not need (but in general yes, you have to use quotes).
Quote:

Can sed and awk understand shorthand regex?
grep knows 3 different kind of regex, sed knows at least 2, awk has its own style, but more or less similar....
perl has its own PCRE and python knows that too.

Turbocapitalist 04-10-2017 01:10 AM

Quote:

Originally Posted by MBoyle19 (Post 5694997)
I didn't know the exact name they were called. So I called it regex types. Tobyv did mentioned above it was called short hand regex.

Can sed and awk understand shorthand regex?

syg00 mentioned them above in #2 and #4. They are perl regular expressions, ported to other languages this is often called perl-compatible regular expression (PCRE). Within perl they are properly called metasymbols and they stand in for some functionality as well as pattern data. You'll find PCRE nearly everywhere these days.

But as to whether sed and awk can understand perl's regular expression metasymbols, the answer is, "no" at least for existing versions to-date.

If you find them useful, then you can get a lot out of one-liners in perl. See the options -e, -i, -n, and -p in
Code:

man perlrun
Anything you can do with sed and awk you can do, plus more, in perl.


All times are GMT -5. The time now is 08:36 PM.