LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-27-2004, 10:36 AM   #1
duerra
Member
 
Registered: Oct 2003
Location: Where No One's Been... Spiral Out!
Distribution: FreeBSD
Posts: 201

Rep: Reputation: 30
Find and Replace?


Are there any command-line text editors with a find and replace feature? I am using ee, pico, and vi, but I have yet to see a find and replace feature in any of them.

Thanks
 
Old 01-27-2004, 10:48 AM   #2
MarkusMan
LQ Newbie
 
Registered: Jan 2004
Posts: 22

Rep: Reputation: 15
sed is your friend

At the command prompt:

#sed -e 's/<find string>/<replace string>/g' <filename> > <newfilename> ; mv <filename> <filename.orig> ; mv <newfilename> <filename>

Hope this helps!

Or actually within vi you can go to command mode and type:

1,$s/<find string>/<replace with string>/g

1 = start at beginning
$ = go to end
s = substitute
g = globally (meaning all)

You can use a . instead of the 1 or $ to designate stopping or starting at the point where your prompt is also.

Last edited by MarkusMan; 01-27-2004 at 10:51 AM.
 
Old 01-27-2004, 10:52 AM   #3
duerra
Member
 
Registered: Oct 2003
Location: Where No One's Been... Spiral Out!
Distribution: FreeBSD
Posts: 201

Original Poster
Rep: Reputation: 30
Wonderful!

Thank you =)
 
Old 01-27-2004, 11:25 AM   #4
Gnuru
Member
 
Registered: Jan 2004
Posts: 53

Rep: Reputation: 15
Quote:
Originally posted by MarkusMan
sed is your friend

At the command prompt:

#sed -e 's/<find string>/<replace string>/g' <filename> > <newfilename> ; mv <filename> <filename.orig> ; mv <newfilename> <filename>
Far too much typing!!!!

Try this:

perl -pi -e 's/find string/replace string/g' <file name>
 
Old 01-27-2004, 11:31 AM   #5
MarkusMan
LQ Newbie
 
Registered: Jan 2004
Posts: 22

Rep: Reputation: 15
Can you do that within vi?
 
Old 01-27-2004, 11:46 AM   #6
duerra
Member
 
Registered: Oct 2003
Location: Where No One's Been... Spiral Out!
Distribution: FreeBSD
Posts: 201

Original Poster
Rep: Reputation: 30
The vi command worked
 
Old 01-27-2004, 06:16 PM   #7
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
lol i guess thats why they are the advanced text editors, cuse u gotta do all that typing , and are hard to learn..
 
Old 01-27-2004, 06:19 PM   #8
MarkusMan
LQ Newbie
 
Registered: Jan 2004
Posts: 22

Rep: Reputation: 15
Technically you can just do

#sed -e 's/<find string>/<replace string>/g' <filename> > <newfilename>

OR

1,$s/ <find string>/<replace string>/g

within vi.

Unless you want to split hairs this is as easy as the perl version. I was giving him a way to back up the file in case something got hosed also, hence the extra information.
 
Old 01-28-2004, 04:01 AM   #9
Gnuru
Member
 
Registered: Jan 2004
Posts: 53

Rep: Reputation: 15
Quote:
Originally posted by MarkusMan
I was giving him a way to back up the file in case something got hosed also, hence the extra information.
You can also do this easily in perl:

# perl -pi'.orig' -e 's/find string/replace string/g' <file name>

The original unmodified file will be saved with the '.orig' suffix!
 
Old 01-28-2004, 04:07 AM   #10
Gnuru
Member
 
Registered: Jan 2004
Posts: 53

Rep: Reputation: 15
Re: Find and Replace?

Quote:
Originally posted by duerra
Are there any command-line text editors with a find and replace feature? I am using ee, pico, and vi, but I have yet to see a find and replace feature in any of them.
Hi Duerra, by command-line, do you mean console?

If so emacs has find and replace that you can access by:

M-x replace-string

or

M-x replace-regexp
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 04:50 AM
find and replace happy78 Programming 11 09-10-2005 10:21 AM
Help - how to find and replace in Vim stardotstar Linux - Software 7 10-14-2004 11:31 PM
Find & Replace Benr Linux - Newbie 5 05-03-2004 02:37 PM
find with grep and replace it dominant Linux - Newbie 4 03-03-2004 01:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:37 AM.

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