General discussion

Locked

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.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

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

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.

Collapse -

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

This question was closed by the author

Back to Linux Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums