Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 04-14-2006, 03:59 AM   #1
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Rep: Reputation: 15
bash replace all matches of regex substring in string

Hi yall, i've been searching and testing for a while, but can't find a way to replace all instances of a regex substring in a string, just with bash. I found this:
given this:
temp=<year>2006</year> <month>04</month>
when i run this:
echo ${temp//[^digit]/}
it prints out:

I want to remove all characters except for numbers. Is this possible?

Last edited by nickleus; 04-14-2006 at 04:07 AM.
Old 04-14-2006, 04:07 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
Check this:
chris@kermit ~ $ experiment=supercall2345i7fragilisticexpi6a7lad22o3c5ious
chris@kermit ~ $ echo ${experiment//[0-9]/}
chris@kermit ~ $ echo ${experiment//[^0-9]/}
chris@kermit ~ $
Old 04-14-2006, 04:08 AM   #3
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Original Poster
Rep: Reputation: 15
crap, i can't believe i didn't try 0-9. thanks chris!

Last edited by nickleus; 04-14-2006 at 04:12 AM.
Old 04-30-2011, 11:08 AM   #4
LQ Newbie
Registered: Apr 2011
Location: Salem Oregon USA
Posts: 3

Rep: Reputation: 1
I know this is a very old post but I stumbled upon it and wanted to offer an alternative to anyone else that stumbles on it.

tmp="<year>2008</year> <month>08</month>"
echo ${tmp//[^[:digit:]]}

When using the named character groups they need to be wrapped in [: :]. The original post just used the word "digit" so any character that didn't match d, i, g or t was removed. "t" was the only letter that matched in the original string (one in each "month" tag) so that is why "tt" was returned.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
replace a substring with another string in C zeppelin Programming 21 11-09-2009 09:59 PM
Regex Question: Only print part of line that matches TheMeteorPolice Programming 5 01-12-2006 01:21 PM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 06:52 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:04 PM.

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