# Text substitution (like sed) with Julia

**URL:** <https://discourse.julialang.org/t/text-substitution-like-sed-with-julia/56684>\
**Category:** General Usage\
**Created:** [March 7, 2021, 4:15pm UTC](https://discourse.julialang.org/t/text-substitution-like-sed-with-julia/56684 "2021-03-07T16:15:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [March 7, 2021, 4:15pm UTC](https://discourse.julialang.org/t/text-substitution-like-sed-with-julia/56684/1 "2021-03-07T16:15:26Z")

</div>

If I want to replace all occurrences of “apple” by “orange” in a file, I can use the Unix shell command

```
sed -i 's/apple/orange/g' filename.txt

```

What would be an equivalent Julia code?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 7, 2021, 4:17pm UTC](https://discourse.julialang.org/t/text-substitution-like-sed-with-julia/56684/2 "2021-03-07T16:17:53Z")

</div>

Start with [Strings · The Julia Language](https://docs.julialang.org/en/v1/base/strings/#Base.replace-Tuple%7BAbstractString,Pair%7D)

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [March 7, 2021, 5:15pm UTC](https://discourse.julialang.org/t/text-substitution-like-sed-with-julia/56684/3 "2021-03-07T17:15:46Z")

</div>

maybe create a regex literal `r"<regex>"`, like this:

```julia
julia> occursin(r"(?i)apple|orange", "Orange or Apple")
true

julia> replace("Orange or Apple", r"(?i)apple" => "Orange")
"Orange or Orange"

```

Or even fancier:

```julia
julia> replace("Orange or Apple", r"(?i)apple" => titlecase("orange"))
"Orange or Orange"

```
