LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 10-14-2004, 06:38 PM   #1
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Rep: Reputation: 30
Help - how to find and replace in Vim


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.

Will.*
 
Old 10-14-2004, 06:48 PM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
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!
 
Old 10-14-2004, 06:49 PM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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.
 
Old 10-14-2004, 07:00 PM   #4
darthtux
Senior Member
 
Registered: Dec 2001
Location: 35.7480° N, 95.3690° W
Distribution: Debian, Gentoo, Red Hat, Solaris
Posts: 2,070

Rep: Reputation: 47
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

Last edited by darthtux; 10-14-2004 at 07:01 PM.
 
Old 10-14-2004, 07:18 PM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
darthtux: check out my command line again, and check out -i in the manpage

You might want to write "Kick me" in the comments to your script (I know I did before I found -i)
 
Old 10-14-2004, 07:33 PM   #6
darthtux
Senior Member
 
Registered: Dec 2001
Location: 35.7480° N, 95.3690° W
Distribution: Debian, Gentoo, Red Hat, Solaris
Posts: 2,070

Rep: Reputation: 47
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.
 
Old 10-14-2004, 10:20 PM   #7
stardotstar
Member
 
Registered: Nov 2002
Location: /au/qld/bne/4157
Distribution: Gentoo mactel-linux
Posts: 238

Original Poster
Rep: Reputation: 30
This is great advice and very straightforward and helpful. Thanks all - I will try it now.

Will
 
Old 10-14-2004, 11:31 PM   #8
idaho
Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: RedHat, Libranet
Posts: 438

Rep: Reputation: 30
A good way to get a handle on vi is to run the tutorial: vimtutor
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace happy78 Programming 11 09-10-2005 10:21 AM
replace a word in vim editor fssengg Linux - Newbie 3 03-10-2005 08:39 AM
vim search and replace - split line phreak7t7 Programming 2 08-16-2004 05:02 PM
Replace word in VIM ICO Linux - Software 2 03-17-2004 01:28 AM
Find and Replace? duerra Linux - General 9 01-28-2004 04:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration