# Read and edit multiple files using a while loop

**URL:** <https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297>\
**Category:** Shell Programming and Scripting\
**Created:** [August 27, 2010, 5:19am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297 "2010-08-27T05:19:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyberfrog](https://community.unix.com/letter_avatar/cyberfrog/32/6_22734026db12803ebb3e059622ac3534.png) [@cyberfrog](https://community.unix.com/u/cyberfrog)\
**Post date:** [August 27, 2010, 5:19am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/1 "2010-08-27T05:19:02Z")

</div>

Hi all,  
I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands:

```nohighlight
:1,$s/search_str/replace_str/g<return>

```

but I am not sure how to automate the \<return\> of these vis commands when I am putting this in a while loop.

i.e. if a loop the first time I cannot actually initiate the above command without pressing the return button, is there a special command for this?

Thanks

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [August 27, 2010, 5:34am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/2 "2010-08-27T05:34:05Z")

</div>

The syntax you use can be also used in sed. If your sed has the -i option (perl has an option like this too), you can just edit the files directly. Else you might want to write the output of sed to a tmp file and mv it back to the original file.  
If you can print the files with a find and hand them over to sed -i with -exec or xargs for example.  
If you feed the filenames from a file, then a while loop may be a good idea.

---

<div class="post-metadata">

**Author:** ![cyberfrog](https://community.unix.com/letter_avatar/cyberfrog/32/6_22734026db12803ebb3e059622ac3534.png) [@cyberfrog](https://community.unix.com/u/cyberfrog)\
**Post date:** [August 27, 2010, 6:08am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/3 "2010-08-27T06:08:41Z")

</div>

Sorry not too familiar with SED commands so breaking this down so I have tried this comamnd:

```nohighlight
sed -e '1,$s/old string/new string/g' myfirstfileinloop.txt

```

now when I execute this it prints to the terminal including the new strings that were replaced yet when I open the actual file it still remains the old same and nothing has changed?

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [August 27, 2010, 6:23am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/4 "2010-08-27T06:23:14Z")

</div>

Ok!

We have two options:

1. As zaxxon commented, if your sed supports the "-i" option, in place change, use it.

```nohighlight
sed -i -e '1,$s/old string/new string/g' myfirstfileinloop.txt

```

1. If it does not, you must redirect the output, like the following:

```nohighlight
ls -1 | while read fname
do
      myFileTmp=${fname}.tmp
      sed -e '1,$s/old string/new string/g' "${fname}" > "${myFileTmp}"
      # If you want to remove the original file uncomment the line below
      [$? -eq 0] && rm -f "${fname}"
done

```

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [August 27, 2010, 6:26am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/5 "2010-08-27T06:26:50Z")

</div>

The second option will change all files in a directory, you want one file, just:

```nohighlight
sed -e '1,$s/old string/new string/g' myfirstfileinloop.txt > myfirstfileinloop.txt.new

```

Also, in the second option you can rename the tmp file to the original one:

```nohighlight
ls -1 | while read fname
do
      myFileTmp=${fname}.tmp
      sed -e '1,$s/old string/new string/g' "${fname}" > "${myFileTmp}"
      # If you want to change back to the original file uncomment the line below
      # [$? -eq 0] && mv "${myFileTmp}" "${fname}"
done

```

---

<div class="post-metadata">

**Author:** ![cyberfrog](https://community.unix.com/letter_avatar/cyberfrog/32/6_22734026db12803ebb3e059622ac3534.png) [@cyberfrog](https://community.unix.com/u/cyberfrog)\
**Post date:** [August 27, 2010, 6:26am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/6 "2010-08-27T06:26:50Z")

</div>

It supports sed but it seems to throw this illegal message:

```nohighlight
sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]

```

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [August 27, 2010, 6:28am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/7 "2010-08-27T06:28:54Z")

</div>

It is because sed does not support the: "-i" option. Try the second option.

Also, in what platform are you executing it?

Regards.

---

<div class="post-metadata">

**Author:** ![cyberfrog](https://community.unix.com/letter_avatar/cyberfrog/32/6_22734026db12803ebb3e059622ac3534.png) [@cyberfrog](https://community.unix.com/u/cyberfrog)\
**Post date:** [August 27, 2010, 7:04am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/8 "2010-08-27T07:04:34Z")

</div>

will try, the platform is HP-UX

---------- Post updated at 03:04 AM ---------- Previous update was at 02:36 AM ----------

Its the HP Unix platform

Regards

---

<div class="post-metadata">

**Author:** ![felipe.vinturin](https://community.unix.com/user_avatar/community.unix.com/felipe.vinturin/32/1840_2.png) [@felipe.vinturin](https://community.unix.com/u/felipe.vinturin)\
**Post date:** [August 27, 2010, 8:01am UTC](https://community.unix.com/t/read-and-edit-multiple-files-using-a-while-loop/272297/9 "2010-08-27T08:01:42Z")

</div>

Were you able to test it?
