# Help with search and replace

**URL:** <https://community.unix.com/t/help-with-search-and-replace/327665>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [April 26, 2013, 6:16pm UTC](https://community.unix.com/t/help-with-search-and-replace/327665 "2013-04-26T18:16:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samnyc](https://community.unix.com/letter_avatar/samnyc/32/5_5575768a8748004e209b776fc1b2916d.png) [@samnyc](https://community.unix.com/u/samnyc)\
**Post date:** [April 26, 2013, 6:16pm UTC](https://community.unix.com/t/help-with-search-and-replace/327665/1 "2013-04-26T18:16:57Z")

</div>

Hi, I am using vi. I need help with search and replace.

I have a file close to 100 lines. I want to search /var and replace with rm /var.

```nohighlight
/var/sadm/pkg/SUNWcsu/save/125378-03/undo.Z
/var/sadm/pkg/SUNWcsr/save/pspool/SUNWcsr/save/127718-02/undo.Z
/var/sadm/pkg/SUNWcsr/save/127718-02/undo.Z

```

My results should look like this..

```nohighlight
rm /var/sadm/pkg/SUNWcsu/save/125378-03/undo.Z
rm /var/sadm/pkg/SUNWcsr/save/pspool/SUNWcsr/save/127718-02/undo.Z
rm /var/sadm/pkg/SUNWcsr/save/127718-02/undo.Z

```

Please help. I did :s didn't work. the rm word comes before /

---

<div class="post-metadata">

**Author:** ![hanson44](https://community.unix.com/letter_avatar/hanson44/32/5_5575768a8748004e209b776fc1b2916d.png) [@hanson44](https://community.unix.com/u/hanson44)\
**Post date:** [April 26, 2013, 6:20pm UTC](https://community.unix.com/t/help-with-search-and-replace/327665/2 "2013-04-26T18:20:35Z")

</div>

```nohighlight
:1,$s!/var!rm /var!g

```

---

<div class="post-metadata">

**Author:** ![stuckinboredom](https://community.unix.com/letter_avatar/stuckinboredom/32/5_5575768a8748004e209b776fc1b2916d.png) [@stuckinboredom](https://community.unix.com/u/stuckinboredom)\
**Post date:** [April 26, 2013, 6:22pm UTC](https://community.unix.com/t/help-with-search-and-replace/327665/3 "2013-04-26T18:22:20Z")

</div>

Try this:

```nohighlight
sed -e 's/^\/var/rm &/g' <file>  

```

---

<div class="post-metadata">

**Author:** ![hanson44](https://community.unix.com/letter_avatar/hanson44/32/5_5575768a8748004e209b776fc1b2916d.png) [@hanson44](https://community.unix.com/u/hanson44)\
**Post date:** [April 26, 2013, 6:22pm UTC](https://community.unix.com/t/help-with-search-and-replace/327665/4 "2013-04-26T18:22:44Z")

</div>

The / separator is arbitrary. You can use a different separator such as ! as I used in this case.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [April 27, 2013, 2:32am UTC](https://community.unix.com/t/help-with-search-and-replace/327665/5 "2013-04-27T02:32:12Z")

</div>

Substitute /var at the beginning of the line only!

```nohighlight
:1,$s!^/var!rm /var!

```
