LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-28-2011, 01:13 PM   #1
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 70

Rep: Reputation: 1
REGEXP in VIM


Hi!

I am trying to learn VIM and regular expressions.
Got stuck when trying to add line numbers to a Heidenhain CNC file.

Example:
Before.
Code:
;Not code
#Not code

L X+0.000 Y+0.000 F100 M
C X+10.000 Y-15.000 F M
CP IA+60.000 F M
Now I want to add line numbers to every line that starts with a capital letter.

After.
Code:
;Not code
#Not code

0 L X+0.000 Y+0.000 F100 M
1 C X+10.000 Y-15.000 F M
2 CP IA+60.000 F M
Here is what i thought would work:
Code:
:%s/\(^\)\([A-Z]*\)\2/\=line('.')-1 . ' '
%s - preform substitute on whole file

\(^\) - add "beginning" of the line to first "backreference group"

\([A-Z]*\) - add the rest of a valid line to secound "backreference group"

\2/\=line('.')-1 . ' ' - substitute the first backreference with line number and whitespace


This prints line number on all lines.

I cant figure out how to add the secound backreference as a condition. Like: only preform substitute if \3 exists.


Any help is appreciated.


//John

Last edited by john_erlandsson; 12-28-2011 at 01:26 PM.
 
Old 12-28-2011, 01:17 PM   #2
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 70

Original Poster
Rep: Reputation: 1
This is what i started with:
Code:
:%s/^/\=line('.')-1 . ' '
It gives the same result.
 
Old 12-28-2011, 03:58 PM   #3
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 70

Original Poster
Rep: Reputation: 1
This is almost correct:
Code:
%g/^[A-Z]/s/^/\=line('.')-1 . ' '
Gives the output:
Code:
;Not code
#Not code

3 L X+0.000 Y+0.000 F100 M
4 C X+10.000 Y-15.000 F M
5 CP IA+60.000 F M
 L X+0.000 F M
7 L X+0.000 Y+0.000 F M
Is it possible to make the line() function count only the lines matched in the first expression?
 
Old 12-29-2011, 06:40 AM   #4
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 70

Original Poster
Rep: Reputation: 1
I solved it:

Code:
let X=-1|%g/^[A-Z]/let X=X+1|s/^/\=X
Here is how i think it works:

let X=-1 Declare variable X and initialize it to -1

|%g/^[A-Z] Pipe to new command and search entire file for lines that starts with a capital letter

/let X=X+1 Increment the variable X when a match is found...

|s/^/\=X ...and print the number at the beginning of that line
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Personalize and Optimize Vim editor using Packtís new Vim 7.2 book LXer Syndicated Linux News 0 05-20-2010 11:20 PM
Switching from vim to vim -g from inside vim iDragoon Linux - Software 4 05-15-2009 12:46 PM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 05:26 PM
vim or sed multiline regexp matching eentonig Programming 1 09-08-2008 10:06 AM
LXer: Cream for Vim - Making Vim more user friendly LXer Syndicated Linux News 0 06-18-2006 12:54 AM


All times are GMT -5. The time now is 04:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration