LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-09-2007, 07:05 PM   #1
ShaqDiesel
Member
 
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, 07:24 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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

Cheers,
Tink
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 AM.

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
Open Source Consulting | Domain Registration