Share your knowledge at the LQ Wiki.
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 02-09-2007, 08:05 PM   #1
Registered: Jul 2005
Posts: 144

Rep: Reputation: 15
sed regex question

I am using using the following regex string
regex="letter1.letter1.letter1" in a ksh script. The dots are supposed to be metacharacters that would match any single character.

I was hoping sed -i "s/($regex)/hello$1/g file1 would transform
"letter1 letter1 letter1" to "hello letter1 letter1 letter1" in file1 but it didn't do anything. I'd like to mention that I can't just use a literal in the substitute statement because it isn't always "letter1". What's going on here? Thanks in advance.
Old 02-09-2007, 08:24 PM   #2
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
Try this:
[tink:~]$ regex="letter1.letter1.letter1"
[tink:~]$ sed "s/\($regex\)/hello\1/g" file1
helloletter1 letter1aletter1
letter1 letter2 letter1
where file1 looks like
$ cat file1
letter1 letter1aletter1
letter1 letter2 letter1



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 RegEx problems InJesus Programming 6 01-12-2007 12:48 PM
regex question Toadman Linux - General 0 12-30-2005 01:59 PM
sed / regex question whysyn Linux - General 3 06-28-2005 03:11 PM
regex problem with sed ta0kira Programming 7 06-20-2005 01:33 AM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 02:09 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 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
Open Source Consulting | Domain Registration