The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
In Help, Substitute Text ... solidhelix08 Shell Programming and Scripting 6 02-07-2008 02:21 AM
rm substitute with blacklist broli UNIX for Dummies Questions & Answers 2 12-06-2007 05:13 AM
Substitute Variable mmg2711 UNIX for Dummies Questions & Answers 0 10-26-2007 03:59 AM
Substitute File name vanand420 Shell Programming and Scripting 22 03-13-2007 11:40 PM
vi + regexp + substitute Lomic UNIX for Dummies Questions & Answers 9 12-10-2004 07:50 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-11-2007
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
Substitute in vi

I know in vi you can do

:%s/replaceme/withthis/

but if i want to find all lines say without a # at the begining and I want to put it in how would that command be formatted? I can't figure it out for the life of me.

#comment
blah1
hey1
grrr1

#comment
#blah1
#hey1
#grrr1
Reply With Quote
Forum Sponsor
  #2  
Old 01-11-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
Code:
:/^[^#]/s/replaceme/withthis/
Reply With Quote
  #3  
Old 01-16-2007
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
i can't get it to work what do i need to put into the /s to get this to work....been trying everything
Reply With Quote
  #4  
Old 01-16-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
Quote:
Originally Posted by kingdbag
i can't get it to work what do i need to put into the /s to get this to work....been trying everything
this is NOT helping us to help you....
Reply With Quote
  #5  
Old 01-16-2007
Supporter
 

Join Date: Jul 2006
Posts: 156
Code:
:%s/replaceme/withthis/
Change to.

Code:
:%s/^\([^#]\)/#\1/

You should lookup regular expressions. The ^ character matches "beginning of line". The square brackets define a character class. Basically, it means, "match any character in the square brackets". The ^ character has a special meaning inside the character class only if it's the 1st character in the character class. It means NOT.

So this means " find any line that starts with anything except a '#' character, and replace that character with a '#' character ".

The parentheses "capture" a regular expression match. In vi, they must be escaped so that vi knows you want to capture, instead of trying to match the parentheses. The \1 notation in the replacement string means replace with the 1st "captured" match.
Reply With Quote
  #6  
Old 01-17-2007
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
thanks for the help...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0