LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-12-2017, 02:34 PM   #1
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Rep: Reputation: 46
VIM: how do I make this substitution?


If a line starts with "A. " (no quotes) then I want the next line to start with "B. ".

For example:

I want to change this

A. blah blah blah
yada yada yada

to

A. blah blah blah
B. yada yada yada
 
Old 04-12-2017, 07:04 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Using the substitute command:
  1. Use a capture group to get the lines starting with "A. " and ending with a new line.
  2. Replace with capture group + "B. "

In case you need it, here's a hint on how to include the end of line: http://vim.wikia.com/wiki/Search_across_multiple_lines

Give it a shot. If you have problems post the command(s) you tried.
 
Old 04-13-2017, 05:47 PM   #3
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Original Poster
Rep: Reputation: 46
Maybe I was not clear, because that does not answer my question at all.

This is not a simple substitution.

This is not:

:%s/foo/bar/g

Finding the string to replace is easy. But how do I replace the string without replacing everything?

I do not want to change what is presently on the line starting "A. " I just want add a "B. " on the next line.
 
Old 04-13-2017, 06:39 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by walterbyrd View Post
Maybe I was not clear, because that does not answer my question at all.
Apparently, you didn't look at that link and/or don't understand regex capture groups because both of these work for me:
Code:
:%s/\(A\..*\_s\)/\1B\. /g
:%s/A\..*\_s\zs\ze/B\. /g
 
Old 04-13-2017, 07:30 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319
This is my attempt, which works for the two-line example:

Code:
%s/\v(A. .*\n)(.*)$/\1B. \2/g
 
Old 04-13-2017, 07:57 PM   #6
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
@dugan - After looking at your solution I see that it is not necessary to escape the periods, thus mine can be shortened a little:
Code:
:%s/\(A..*\_s\)/\1B. /g
:%s/A..*\_s\zs\ze/B. /g
My motto: when in doubt escape, readability be damned!
 
Old 05-06-2017, 02:25 PM   #7
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Original Poster
Rep: Reputation: 46
This worked:

Code:
%s/\v(A. .*\n)(.*)$/\1B. \2/g
And this worked

Code:
%s/\(A..*\_s\)/\1B. /g
I am going to have to do some research to find out way. But I'm fine with that.

Thanks for the answers.
 
Old 05-06-2017, 03:47 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319
You don't have to do as much escaping if you start the pattern with \v. It turns on something called "very magic mode".

I recommend using it whenever you use vim's version of regex.
 
Old 05-10-2017, 04:03 PM   #9
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Original Poster
Rep: Reputation: 46
Thank you.

I was wonder what that \v was.
 
Old 01-04-2018, 02:06 PM   #10
walterbyrd
Member
 
Registered: Apr 2004
Posts: 734

Original Poster
Rep: Reputation: 46
FWIW:

After playing around with this, I have found that it's better to use the backslash before the dots. I also use a carrot before the A.

I use this now:

Code:
:%s/\(^A\. .*\)\n/\1\rB. /
Of course there are a lot of ways to do this. Some methods may not require a backslash before the dot.

The way I used to do this, has caused some trouble.

Code:
%s/\v(A. .*\n)(.*)$/\1B. \2/g
or

Code:
%s/\(A..*\_s\)/\1B. /g
In either of those methods, if the line started with An or an As, it might not work.

With the second method, if the line had an A[any char] anywhere in the line, it would put a B at the beginning of the next line.
 
Old 01-04-2018, 02:10 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319
Quote:
Originally Posted by walterbyrd View Post
carrot
caret

:^)
 
  


Reply

Tags
vim



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
[SOLVED] How to make Vim (scripts) write to stdout? (or, how to use Vim as a filter program?) dedec0 Linux - Software 13 02-04-2013 05:45 AM
[SOLVED] sed: Match one line, make a substitution a few lines down? ShadowCat8 Programming 6 06-08-2011 07:59 PM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 04:26 PM
Vim Substitution (specialized) infidel Linux - Software 7 05-07-2005 04:28 PM
help w/ Vim :make leb01002 Linux - Software 1 11-12-2003 12:07 PM

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

All times are GMT -5. The time now is 01:44 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