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.
I have a text file and I would like to replace a name for another, the name is Daniel and I would like to replace all instances of Daniel for Danny. how can I do this automatically with a command?
Remember that sed does not alter the original file, it just displays the modified output. If you want to put the output in a new file, use bash output redirection.
There are more then one way to do this, here's one using sed:
sed -i.bak 's/Daniel/Danny/g' <textfile>
The s (sed -i.bak 's/Daniel/Danny/g' <textfile>) tells sed that you want to replace, the g on the end makes it global, not just the first one on a line.
sed looks for Daniel (sed -i.bak 's/Daniel/Danny/g' <textfile>),
and put Danny (sed -i.bak 's/Daniel/Danny/g' <textfile>) in its place.
The -i.bak option makes a copy of the original named <textfile>.bak, changes are made in <textfile>
There are more then one way to do this, here's one using sed:
sed -i.bak 's/Daniel/Danny/g' <textfile>
I may be wrong as I am still learning sed but isn't the g option in sed irrelevant. As far as I know, g switch is used for global and sed by default does global changes. g switch is needed for ed(sed's ancestor), which by default only changes the current line. Sed on the other hand changes all the lines by default.
You do need the g if you want to change multiple on the same line (global for that line):
cat infile
Daniel aap noot Daniel
sed 's/Daniel/Danny/' infile
Danny aap noot Daniel
sed 's/Daniel/Danny/g' infile
Danny aap noot Danny
Cannot find this in the manpage, but the Sed&Awk book from O'Reilly (page 80 if you have a copy) also states that 'Normally only the first occurence is replaced.'.
You do need the g if you want to change multiple on the same line (global for that line):
cat infile
Daniel aap noot Daniel
sed 's/Daniel/Danny/' infile
Danny aap noot Daniel
sed 's/Daniel/Danny/g' infile
Danny aap noot Danny
Cannot find this in the manpage, but the Sed&Awk book from O'Reilly (page 80 if you have a copy) also states that 'Normally only the first occurence is replaced.'.
Hope this clears things up.
Awesome...thanks buddy. As I said, I am still learning. I have that Sed & Awk (O'Reilly) book and now that you've told me I think I remember it. Anyway, I better get back to that book . Thanks for clearing that up.
I hasten to point out that almost any text editor or word processor has a search and replace function.
CLI is NOT always the easiest way to do something---although you will certainly learn more that way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.