Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-19-2008, 02:24 AM   #1
LQ Newbie
Registered: Mar 2006
Location: Jamaica Plain (Boston)
Distribution: LFS
Posts: 8

Rep: Reputation: 0
trouble backreferencing with sed

Hi all,

I'm running Fedora8 on an AMD x64 have some iterating text in a flat file I'd like to change, but keep the iteration.

For example, parts of my file say "Ge1:1 then some text, Ge1:2 then some more text...Ge1:22...."

I'd like to replace "Ge1:n" with "<sub>n</sub>" - basically, I'm formatting something in HTML and want to put the number after the colon into superscript and lose the "Ge1:"

I'm reading a sed tutorial (see link below) and came up with the following:

sed s/Ge1\:\([0-9]*\)/\<sub\>\1\<\\/sub\>/ a.txt > b.txt

But it doesn't replace anything.

When I run: sed s/Ge1\:[0-9]*\ /\<sub\>\<\\/sub\>/ a.txt > b.txt it works fine, but I lose "n"

Does anyone know why the "\1" isn't recognizing the "\([0-9]*\)" ?

Thanks, William
Old 06-19-2008, 02:32 AM   #2
Mr. C.
Senior Member
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
You haven't quoted the sed substitution, and the shell is eating the backslashes. Place quotes around your expression. Also, you can use a different substitution field delimiter to reduce the backslashes. Finally, don't backslash characters that don't need to be escaped.

Here's a solution:

sed 's-Ge1:\([0-9]*\)-<sub>\1<\/sub>-'
Note how I changed s/x/y/ into s-x-y-

Last edited by Mr. C.; 06-19-2008 at 09:16 PM.
Old 06-19-2008, 09:13 PM   #3
LQ Newbie
Registered: Mar 2006
Location: Jamaica Plain (Boston)
Distribution: LFS
Posts: 8

Original Poster
Rep: Reputation: 0

Thank you!


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
sed trouble hfawzy Programming 5 08-07-2006 11:37 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
Having some trouble with sed P0ldy Linux - General 6 01-04-2006 05:26 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

All times are GMT -5. The time now is 10:53 AM.

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