![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 ![]() |
|
||||
|
* 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' 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' From here*to here try this Code:
sed 's/From here.*to here/new text/g' |
|
||||
|
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.
* is not the only modifier:
|
|
||||
|
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")
Code:
'some text in front new test at the end' |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|