LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-12-2010, 08:06 PM   #1
simpsonc
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
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,832
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Code:
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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python
Code:
MAP={"1":"one","2":"two","3":"three","4":"four","5":"five",
    "6":"six","7":"seven","8":"eight","9":"nine","0":"zero"}
data=open("file").readlines()
lastline=list(data[-1])
for n,c in enumerate(data[-1]):
    if c.isdigit():
         lastline[n]=MAP[c]
         break
data[-1]=''.join(lastline)
print ''.join(data)
 
Old 09-12-2010, 08:55 PM   #4
simpsonc
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.
 
  


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


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration