Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-12-2010, 08:06 PM   #1
LQ Newbie
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Trying to use sed to replace a number that is in a string with a word

I am trying to replace the first number in a string at the end of the line with the word that matches that number. for example i want to replace 546215 to be five-46215. The command that I have tried is

sed -r 's/5[0-9]{5}$/five-[0-9]{5}/'

I am new to all this so any help would be appricated it.
Old 09-12-2010, 08:15 PM   #2
Senior Member
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,856
Blog Entries: 36

Rep: Reputation: 456Reputation: 456Reputation: 456Reputation: 456Reputation: 456
echo 546215 | sed 's/\(5\)\([0-9]\{5\}\)$/five-\2/'
For some of the normal regex stuff you have to escape it with a backslash. I used regex grouping as well (see Group operator and matching nth group).

Last edited by sag47; 09-12-2010 at 08:21 PM.
Old 09-12-2010, 08:50 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python
for n,c in enumerate(data[-1]):
    if c.isdigit():
print ''.join(data)
Old 09-12-2010, 08:55 PM   #4
LQ Newbie
Registered: Sep 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you

Thank you I tried a command similar to this however i had forgot to include the \5\

Last edited by simpsonc; 09-12-2010 at 10:32 PM.


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
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM
Global Replace A String That's Not A Whole Word onesikgypo Programming 5 09-18-2009 09:06 AM
SED ? get number before specific word czezz Programming 3 08-11-2009 07:00 PM
sed 1st word to replace all instances of ( donnied Programming 5 08-21-2008 07:43 PM
how to replace special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 03:33 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:14 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