Discussion Locked

Replace Text in file Using Shell Script

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

What is the easiest way to replace text in a text file using Shell Script instead of going into vi editor to do that?

I am trying to replace all "1234" to "4321" in a text file.

2 replies

by cpfeiffe

Use:
sed 's/1234/4321/g' file > file.new

Sed replaces text strings (and does some other things). Enclosed in '' is your sed argument. You are saying s(ubstitute) 1234 with 4321 g(lobally). Without the g it will simply replace the first occurrance. You can't replace it in the original file so you need to redirect the output to a new file, but you can then rename the new file 'mv file.new file' to complete the task.

Good luck.

This discussion is closed to new replies.