The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Use wildcards in a script emferrari Shell Programming and Scripting 13 03-07-2008 04:14 AM
wildcards NOT C3000 UNIX for Dummies Questions & Answers 5 11-21-2007 07:19 AM
ls with wildcards benu302000 UNIX for Dummies Questions & Answers 10 06-29-2005 01:53 PM
wildcards benu302000 UNIX for Dummies Questions & Answers 3 06-29-2005 12:10 PM
Wildcards in VI peter.herlihy UNIX for Dummies Questions & Answers 8 01-08-2002 04:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-27-2006
HealthyGuy HealthyGuy is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 8
Wildcards in SED

Hi Folks

Quick one I can't seem to figure out..

I need to replace a string such as "From here.....to here".

I would think the command would look like:
sed 's/From here*to here/new text/g' or
sed 's/From here\*to here/new text/g'

But it's not working for me.

Thanks in advance
  #2 (permalink)  
Old 09-27-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
Code:
echo 'aksjfhd from here fooo asdlfkjaslkfjd to here aslfjdlaksjdf' | sed 's/from here.*to here/OVER THERE/'
  #3 (permalink)  
Old 09-27-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
* in regular expression will match any number (or none) of the single character that immediately precedes it

Code:
sed 's/From here*to here/new text/g'
This regular expression From here*to here will match
From herto here
From hereto here
From hereeto here
...

\ Usually, turn off the special meaning of the following character

Code:
sed 's/From here\*to here/new text/g'
This regular expression From here\*to here will match
From here*to here

try this

Code:
sed 's/From here.*to here/new text/g'
  #4 (permalink)  
Old 09-27-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,909
You might want to use the '-r' option to tell sed to use extended regular expressions. Basic sed regexes are quite limited.

Second, regular expressions work differently in sed than they do in a shell. * doesn't mean anything by itself, it's a modifier for something else. First you tell it what expression you want to match, then optionally, how many of them you want to match. An expression can be a single letter, a set of letters, or something in brackets.
  • A by itself just matches the letter A, like you'd expect.
  • [ABC] by itself just matches the letter A, B, or C.
  • A* tells it to match 0 or more A characters.
  • [ABC]* tells it to match 0 or more characters among A, B, C.
  • [A-Z]* tells it to match 0 or more characters among A, B, C, ..., Z.
  • [^A]* tells it to match 0 or more characters that aren't A.
  • (ABC)* tells it to match 0 or more repititions of "ABC".

* is not the only modifier:
  • A+ tells it to match 1 or more A characters.
  • A? tells it to match 0 or 1 A characters.
  • A{4} tells it to match precisely 4 A characters.
  #5 (permalink)  
Old 09-27-2006
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
The -r option is not available on all Unix flavour.
AIX sed doesn't support this option.

Jean-Pierre.
  #6 (permalink)  
Old 09-27-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
Alternative , without regular expression

Code:
#!/usr/bin/python
string = "some text in front From here in the middle to here at the end"
fromindex = string.index("From here")
toindex = string.index("to here")
tobeReplace = string[ fromindex : toindex + len("to here") ]
string.replace( tobeReplace , "new text")
Output:
Code:
'some text in front new test at the end'
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0