LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-09-2017, 08:34 PM   #1
MBoyle19
LQ Newbie
 
Registered: Apr 2017
Posts: 10

Rep: Reputation: Disabled
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!

Last edited by MBoyle19; 04-09-2017 at 08:39 PM.
 
Old 04-09-2017, 08:51 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,773

Rep: Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571
There are several variations in regex - regex ain't regex.

-E gets you extended regex, -P gets you PCRE.
 
1 members found this post helpful.
Old 04-09-2017, 09:25 PM   #3
MBoyle19
LQ Newbie
 
Registered: Apr 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
There are several variations in regex - regex ain't regex.

-E gets you extended regex, -P gets you PCRE.
?????
 
Old 04-09-2017, 09:36 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,773

Rep: Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571
Code:
man grep
man 7 regex
man perlre
 
2 members found this post helpful.
Old 04-09-2017, 09:44 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 17,496
Blog Entries: 28

Rep: Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433Reputation: 5433
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.")

Last edited by frankbell; 04-09-2017 at 09:45 PM.
 
1 members found this post helpful.
Old 04-09-2017, 09:57 PM   #6
TobyV
LQ Newbie
 
Registered: Apr 2017
Posts: 19

Rep: Reputation: Disabled
Quote:
Originally Posted by MBoyle19 View Post
?????
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'
 
1 members found this post helpful.
Old 04-10-2017, 12:12 AM   #7
MBoyle19
LQ Newbie
 
Registered: Apr 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
I have one more problem.

Code:
a="abc [ 123 ]"

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

abc   123
How to suppress the extra white space?

Last edited by MBoyle19; 04-10-2017 at 12:13 AM.
 
Old 04-10-2017, 12:21 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
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 ???)
 
Old 04-10-2017, 12:24 AM   #9
MBoyle19
LQ Newbie
 
Registered: Apr 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
I want it to look like this abc 123 using bash
 
Old 04-10-2017, 12:28 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
Code:
echo ${a//[^a-zA-Z0-9]/ }
(no " )
 
1 members found this post helpful.
Old 04-10-2017, 12:40 AM   #11
MBoyle19
LQ Newbie
 
Registered: Apr 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 04-10-2017, 12:45 AM   #12
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
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
 
1 members found this post helpful.
Old 04-10-2017, 12:53 AM   #13
MBoyle19
LQ Newbie
 
Registered: Apr 2017
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sefyir View Post
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?

Last edited by MBoyle19; 04-10-2017 at 12:56 AM.
 
Old 04-10-2017, 01:09 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
Quote:
Originally Posted by MBoyle19 View Post
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 View Post
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.
 
1 members found this post helpful.
Old 04-10-2017, 01:10 AM   #15
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Quote:
Originally Posted by MBoyle19 View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] trouble with a grep regex r.stiltskin Programming 9 01-04-2015 05:45 PM
[SOLVED] RegEx in grep confusion... mddnix Linux - Newbie 11 05-26-2013 07:07 PM
REGEX with grep Anna1987 Linux - Newbie 9 03-09-2013 07:56 AM
grep regex with wildcard yknot7 Linux - Newbie 6 11-10-2011 01:11 AM
regex in ls vs. grep jhwilliams Linux - Software 2 08-10-2007 10:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration