Linux command to find and replace string within text file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Why do you say that? You can put anything you want in the SED syntax. Do mean path in the "s" command, or the path in the filename? (In this context, I suspect the latter)
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 "/"
Why not? sed accepts file names with or without a relative or absolute path, as all the linux commands which accept a file name as argument. Otherwise you can always use the cd command, but it depends only on what is more comfortable for you.
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).
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.