LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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

Closed Thread
 
Search this Thread
Old 03-01-2008, 09:14 AM   #1
yusufs
Member
 
Registered: Oct 2007
Posts: 162

Rep: Reputation: 30
editor search and replace


Dear all,


I have a big file like which contains /mnt//oracle in most of the places .. I want to know in VI editor, can we search for the string // and replace with / ..such that mnt//oracle will become /mnt/oracle. I know to query for a word in VI.. but for a wildcard like this how to finf out and replace ?


Thanks
Yusuf
 
Old 03-01-2008, 09:30 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
%s/\/\//\//g
 
Old 03-01-2008, 03:14 PM   #3
alioop
Member
 
Registered: Mar 2004
Location: Houston,Texas
Distribution: Via Debian!!!
Posts: 113

Rep: Reputation: 21
Quote:
Originally Posted by ghostdog74 View Post
Code:
%s/\/\//\//g
What in hades does this mean?
 
Old 03-01-2008, 04:06 PM   #4
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by alioop View Post
What in hades does this mean?
It means search for // and replace it with /
 
Old 03-01-2008, 04:16 PM   #5
slackhack
Senior Member
 
Registered: Jun 2004
Distribution: Arch, Debian, Slack
Posts: 1,016

Rep: Reputation: 46
lol, I use vim all the time, and I have to say that's got to be the weirdest most arcane and ridiculous command I've ever seen. nothing should be that counterintuitive and absurd, having to type a bunch of slashes and backslashes in a row is not my idea of usability or even readability. the vim creators are such geniuses that they can't just make it something like s/<string> r/<string>, or whatever?

btw, I tried it and it doesn't even work. when I type the % it doesn't do anything and if I type : first, it says % not an editor command.

anyway, that just struck me as needlessly and amusingly excessive and arcane.

OP: I would recommend opening gedit or leafpad or whatever you might use in KDE and doing it that way.
 
Old 03-01-2008, 04:44 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,356

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I only use vi when I absolutely have to - and certainly not for a task such as requested. That's what sed does.
But you'll all have to get your heads around regex to use it.
Does "s://:/:g" make you feel more comfortable ???
 
Old 03-01-2008, 04:45 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by slackhack View Post
the vim creators are such geniuses that they can't just make it something like s/<string> r/<string>, or whatever?
The vim creators are keeping in tune with the vi/ex creator (Bill Joy) who developed upon the original ed, from whose regular expression syntax the various utilities such as sed, grep, awk, and perl derive. The syntax is:
Code:
[ADDRESS]s/expression/replace/[flags]
The % address means the entire file, and the g flag means global substitution (i.e., not just once). In general, if you have characters such as / in your expression in vi, you have to escape them so the editor knows the difference. You can also use alternate delimiters (in both vi and vim), so the following works as well (this eliminates so-called leaning toothpick syndrome):
Code:
%s#//#/#g
Quote:
Originally Posted by slackhack View Post
btw, I tried it and it doesn't even work. when I type the % it doesn't do anything and if I type : first, it says % not an editor command.
You have to be in ex mode. If you are in NORMAL mode (not INSERT mode), the following should work (if you type it literally and press enter):
Code:
:%s/\/\//\//g
Quote:
Originally Posted by slackhack View Post
anyway, that just struck me as needlessly and amusingly excessive and arcane.

OP: I would recommend opening gedit or leafpad or whatever you might use in KDE and doing it that way.
You are free to use whatever editor you want. The OP asked for instructions in vi and those were provided.
 
Old 03-01-2008, 05:07 PM   #8
slackhack
Senior Member
 
Registered: Jun 2004
Distribution: Arch, Debian, Slack
Posts: 1,016

Rep: Reputation: 46
Quote:
Originally Posted by osor View Post
The vim creators are keeping in tune with the vi/ex creator (Bill Joy) who developed upon the original ed, from whose regular expression syntax the various utilities such as sed, grep, awk, and perl derive. The syntax is:
Code:
[ADDRESS]s/expression/replace/[flags]
The % address means the entire file, and the g flag means global substitution (i.e., not just once). In general, if you have characters such as ‘/’ in your expression in vi, you have to escape them so the editor knows the difference. You can also use alternate delimiters (in both vi and vim), so the following works as well (this eliminates so-called leaning toothpick syndrome):
Code:
%s#//#/#g
You have to be in ex mode. If you are in NORMAL mode (not INSERT mode), the following should work (if you type it literally and press enter):
Code:
:%s/\/\//\//g
You are free to use whatever editor you want. The OP asked for instructions in vi and those were provided.
Sure, except to be accurate, instructions weren't really provided, only an arcane command.

But like I said, I use vim all the time as my main editor -- though i have never done search & replace with it (and now I know why ). at least there's an alternative to the "leaning toothpick" syndrome. Funny that he would be replacing a slash, I guess that made it look a lot worse.
 
Old 03-01-2008, 07:15 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,988
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

The original is here:
http://www.linuxquestions.org/questi...-in-vi-624929/
 
  


Closed Thread


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
Search and replace TL_CLD Slackware 4 12-25-2007 01:45 PM
How to replace text using VI editor ajaz_javeed Linux - Newbie 2 03-31-2007 06:40 AM
How to replace text using VI editor ajaz_javeed Programming 2 03-30-2007 07:01 AM
search & replace raj_sony2001 Linux - General 4 10-05-2006 02:05 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 07:40 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration