# Wildcards in SED

**URL:** <https://community.unix.com/t/wildcards-in-sed/165587>\
**Category:** Shell Programming and Scripting\
**Created:** [September 27, 2006, 10:26am UTC](https://community.unix.com/t/wildcards-in-sed/165587 "2006-09-27T10:26:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HealthyGuy](https://community.unix.com/letter_avatar/healthyguy/32/6_22734026db12803ebb3e059622ac3534.png) [@HealthyGuy](https://community.unix.com/u/HealthyGuy)\
**Post date:** [September 27, 2006, 10:26am UTC](https://community.unix.com/t/wildcards-in-sed/165587/1 "2006-09-27T10:26:31Z")

</div>

Hi Folks

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

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 🙂 🙂

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [September 27, 2006, 10:36am UTC](https://community.unix.com/t/wildcards-in-sed/165587/2 "2006-09-27T10:36:44Z")

</div>

```nohighlight
echo 'aksjfhd from here fooo asdlfkjaslkfjd to here aslfjdlaksjdf' | sed 's/from here.*to here/OVER THERE/'

```

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [September 27, 2006, 10:38am UTC](https://community.unix.com/t/wildcards-in-sed/165587/3 "2006-09-27T10:38:49Z")

</div>

- in regular expression will match any number (or none) of the single character that immediately precedes it

```nohighlight
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

```nohighlight
sed 's/From here\*to here/new text/g'

```

This regular expression From here\_to here will match  
From here_to here

try this

```nohighlight
sed 's/From here.*to here/new text/g'

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [September 27, 2006, 10:43am UTC](https://community.unix.com/t/wildcards-in-sed/165587/4 "2006-09-27T10:43:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [September 27, 2006, 11:03am UTC](https://community.unix.com/t/wildcards-in-sed/165587/5 "2006-09-27T11:03:25Z")

</div>

The -r option is not available on all Unix flavour.  
AIX sed doesn't support this option.

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/6_22734026db12803ebb3e059622ac3534.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 27, 2006, 7:32pm UTC](https://community.unix.com/t/wildcards-in-sed/165587/6 "2006-09-27T19:32:06Z")

</div>

Alternative , without regular expression

```nohighlight
#!/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:

```nohighlight
'some text in front new test at the end'

```
