Linux command to find and replace string within text file
I want to run a .sh script that will find all instances of gggggg in a single text file, and replace it with hhhhhhh.
I have been looking online and am not sure if I need to use grep or sed command and when I use the said command how to format it in my shell script. I was trying to use sed 's/ggggggg/hhhhhhh/g' public/usr/folder/myfile.txt But I think you cant use the path using sed, so within my sh script, can how do I get it to run se within the directory I want, so that I can then simply put the file name, I thought maybe a cd command. Maybe I should be using grep though. |
Quote:
Note that--regardless of where used--pathnames need to be in one of two forms: 1. Relative to the current position 2. Absolute, starting with "/" Quote:
I would go to http://tldp.org and get the Bash Guide for Beginners. Also, some excellent tutorials here: http://www.grymoire.com/Unix/ |
Quote:
Moreover, if you want to edit the file in place, use the -i option of sed (test it on a dummy copy of the same file before actually do). Edit: too late.... |
It was because it was saying something about the path in the error message I was receiving that I said about the path, message from the server, but I probably misunderstood it..
I added the -i and it is now working fine sed -i 's/gggg/hhhhhhhhhhhhh/g' /path/to/file.txt Thanks ever so much, really appreciate it. |
We like happy endings---thanks for the follow-up
|
rpl is a great command for doing this, it even supports doing it recursively
|
All times are GMT -5. The time now is 08:46 AM. |