# What is the best way to search and replace strings in a file?

**URL:** https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879
**Category:** Code
**Created:** [September 21, 2020, 7:53pm UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879 "2020-09-21T19:53:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Shane\_Dev](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shane_dev/32/841_2.png) [@Shane\_Dev](https://discourse.cmake.org/u/Shane_Dev)
#### Post date: [September 21, 2020, 7:53pm UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879/1 "2020-09-21T19:53:49Z")

</div>

Hi,

In my CMake script, I need to modify other source files by searching and replacing specified strings. In my case, the [configure\_file](https://cmake.org/cmake/help/latest/command/configure_file.html) command is not a solution because I have no control over the input file. Previously I used the `file` and [string](https://cmake.org/cmake/help/latest/command/string.html) commands in the following way -

```
file(READ header.h FILE_CONTENTS)
string(REPLACE "old text" "new text" FILE_CONTENTS ${FILE_CONTENTS})
file(WRITE header.h ${FILE_CONTENTS})

```

However this technique appears to strip out semi-colons from the input file.

What is the best way to search and replace strings in a file?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 21, 2020, 10:23pm UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879/2 "2020-09-21T22:23:57Z")

</div>

Try putting quotes around `${FILE_CONTENTS}` in both commands where you use that.

---

<div class="post-metadata">

### Author: ![Shane\_Dev](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shane_dev/32/841_2.png) [@Shane\_Dev](https://discourse.cmake.org/u/Shane_Dev)
#### Post date: [September 22, 2020, 3:52am UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879/3 "2020-09-22T03:52:00Z")

</div>

Thanks Craig, quoting the variable references worked perfectly! I searched for this feature on [https://cmake.org/cmake/help/latest/](https://cmake.org/cmake/help/latest/) but found nothing. Do you where/if this behaviour is documented?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [September 22, 2020, 6:24am UTC](https://discourse.cmake.org/t/what-is-the-best-way-to-search-and-replace-strings-in-a-file/1879/4 "2020-09-22T06:24:46Z")

</div>

It comes from a CMake’s list syntax, which is ;-separated, the fact that arguments passed to CMake commands are basically mashed together into a list, and that those commands take unbounded number of inputs. So you end up with one longer list. Quoting prevents the semicolons in the expansion from being treated as list-element-separators.  
Think:  
`WRITE;header.h;x;y;z`  
vs  
`WRITE;header.h;"x;y;z"`
