Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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 managed to hang the Gnome simple text editor conducting a find and replace operation on a large mysql database.
The sql is about 40MB and I want to search for table names and replace them.
It is an extremely simple operation simply find `xyz_` and replace with `abc_`
I am very inexperienced with Vi and only use it for editing my conf files usually. I understand it is probably the best tool for this and shouldn;t crash out like gedit did.
Any help appreciated. BTW I have scrolled through lots of the help in Vi but the command structure and processes do not make sense to me yet.
In command mode (press Esc key to make sure you are not in insert/edit mode) type
:%s/pattern/substitute_pattern/
Where pattern is the string you want to replace and substitute_pattern what to replace it with.
after it's done just save the file with
:wq!
OK, it is possible to do in vim, but I wouldn't say it's the best tool for the job - the best tool would be sed
In vim, make sure you're in command mode (press ESC a couple of times), and then type:
:%s/xyz_/abc_/g
the colon is for a command, the % is for a range (means the entire file - you could do this over specific lines if you wanted). Everything else is like a sed regex.
To do it with sed on the command line, you could type this:
sed -ie "s/xyz_/abc_/g" sqlfile
you could expand the regular expression to be more specific if xyz_ appears in places you don't want changed.
The problem with sed is it outputs to the screen. It can mess things up if you just try to direct it to a file because it reads and outputs line by line. I have created a shell script to alleviate hassles for me.
You specify script name and any files you want to do the search and replace on. It will prompt for string to replace and then the string you want to replace with. It places output in a temporary file, when sed is finished, it moves back to original file name and removes the temp file.
Code:
#!/bin/bash
# To replace every occurrence of string in all files specified as arguments
echo "Enter text to replaced"
read arg1
echo "Enter text to replace with"
read arg2
for i in "$@"; do
cat $i | sed "s/$arg1/$arg2/g" > $i.temp
mv $i.temp $i
done
CroMagnon, seems like I learn something new every day But I still like to put things in scripts. The easier the better especially if you search and replace qutie a bit.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.