General discussion


Replace Text in file Using Shell Script

By Working IT ·
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.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by cpfeiffe In reply to Replace Text in file Usin ...

sed 's/1234/4321/g' file >

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' to complete the task.

Good luck.

Collapse -

by Working IT In reply to Replace Text in file Usin ...

This question was closed by the author

Related Discussions

Related Forums