Unix/Linux Go Back    


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 !!

Vi search starting from line n --- any trick for this ???

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-28-2013
newbie_01 newbie_01 is offline
Registered User
 
Join Date: May 2009
Last Activity: 26 May 2015, 11:06 PM EDT
Posts: 188
Thanks: 34
Thanked 0 Times in 0 Posts
Vi search starting from line n --- any trick for this ???

Hi all,

It does not seem to be possible to use vi to search from text from line n forward or backward or is there a not well known vi trick to do that?

What I am trying to do is for example I am on line 100 and I want to search from line 100 backward or forward for a specific text. Using / and ? seems to either start from the top and from the bottom and not from which line you are currently in.

Can anyone advise if what I am wanting to do is possible or not?

Thanks in advance.
Sponsored Links
    #2  
Old Unix and Linux 06-28-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 May 2015, 11:15 PM EDT
Location: San Jose, CA, USA
Posts: 6,375
Thanks: 267
Thanked 2,132 Times in 1,823 Posts
Quote:
Originally Posted by newbie_01 View Post
Hi all,

It does not seem to be possible to use vi to search from text from line n forward or backward or is there a not well known vi trick to do that?

What I am trying to do is for example I am on line 100 and I want to search from line 100 backward or forward for a specific text. Using / and ? seems to either start from the top and from the bottom and not from which line you are currently in.

Can anyone advise if what I am wanting to do is possible or not?

Thanks in advance.
The / and ? search commands in vi search forward and backward, respectively from the current line unless you specify a different range to be searched. The commands:

Code:
:line/RE
  and
:line?RE

will search forward and backward, respectively, for the regular expression RE searching from line number line, and the commands:

Code:
cnt/RE
  and
cnt?RE

search forward and backward, respectively, for the cntth occurrence of the regular expression RE searching from the current line.
The Following User Says Thank You to Don Cragun For This Useful Post:
newbie_01 (06-30-2013)
Sponsored Links
    #3  
Old Unix and Linux 06-30-2013
newbie_01 newbie_01 is offline
Registered User
 
Join Date: May 2009
Last Activity: 26 May 2015, 11:06 PM EDT
Posts: 188
Thanks: 34
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Don Cragun View Post
The / and ? search commands in vi search forward and backward, respectively from the current line unless you specify a different range to be searched. The commands:

Code:
:line/RE
  and
:line?RE

will search forward and backward, respectively, for the regular expression RE searching from line number line, and the commands:

Code:
cnt/RE
  and
cnt?RE

search forward and backward, respectively, for the cntth occurrence of the regular expression RE searching from the current line.

Hi Don,

You are absolutely right, I turned on line numbering and it looks like it searches forward to the end and then go back to the top and search for the text.

Thanks again ...
    #4  
Old Unix and Linux 06-30-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 7 April 2015, 6:37 PM EDT
Location: France
Posts: 2,931
Thanks: 81
Thanked 626 Times in 597 Posts
you can also Go to line 100 (makes line 100 your current line)

in command mode :
100G
(if you don't specify a line number, G will bring you at the end of the file)

Then launch your search from there depending whether you want to look forward or backward for your RE:
/RE
or
?RE
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl search and replace - search in first line and replance in 2nd line arvindng Shell Programming and Scripting 3 02-04-2011 09:50 AM
need a cmd to search starting word revenna Shell Programming and Scripting 1 12-11-2008 02:38 AM
modify a particular pattern starting from second line of the search pattern imas UNIX for Dummies Questions & Answers 1 10-12-2008 01:19 PM
modify a particular pattern starting from second line of the search pattern imas UNIX for Dummies Questions & Answers 2 10-12-2008 11:30 AM
how to delete text from line starting pattern1 up to line before pattern2? repudi8or Shell Programming and Scripting 5 04-15-2008 09:25 PM



All times are GMT -4. The time now is 11:42 PM.