LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-14-2006, 03:59 AM   #1
nickleus
Member
 
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:
Code:
${parameter//pattern/string}
given this:
Code:
temp=<year>2006</year> <month>04</month>
when i run this:
Code:
echo ${temp//[^digit]/}
it prints out:
tt

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
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,372

Rep: Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962
Check this:
Code:
chris@kermit ~ $ experiment=supercall2345i7fragilisticexpi6a7lad22o3c5ious
chris@kermit ~ $ echo ${experiment//[0-9]/}
supercallifragilisticexpialadocious
chris@kermit ~ $ echo ${experiment//[^0-9]/}
23457672235
chris@kermit ~ $
 
Old 04-14-2006, 04:08 AM   #3
nickleus
Member
 
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
schworak
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.


#!/bin/bash
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.
 
  


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 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


All times are GMT -5. The time now is 07:41 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration