Help with Vim

C

Caute

Guest
Hello to everyone!

I'm trying to find two commands in Vim which do the following:

The range would be from the 24th line until the end of the text. I want to substitute all the text that is from the beginning of each line until the last slash of that line by the expression "module="" (note the quote after the = symbol)

So let's suppose this is the file from the 24th line:

Code:
the/fox/jumps/over/the/lazy/dog/asd.txt
A/rabbit/runs/crazily/monkey.txt
The/nice/house/in/the/jungle/elephant.txt

The result should be this:

Code:
module="asd.txt
module="monkey.txt
module="elephant.txt

And then another command that adds a " at the end of each line:

Code:
module="asd.txt"
module="monkey.txt"
module="elephant.txt"

Thanks! and I'm looking forward to help others in this site (not now because I'm a newbie and I can hardly write in English since I speak Spanish)
 


Sed is correct. One very long sed command should be all that is needed:

Code:
sed 's/^.*\/\(.*\)$/source=\"\1\"/g' name_of_file

name_of_file would obviously be changed to the name of your file.
 
Oops just noticed the 'source=' should be 'module=' in my code. This also doesn't take into account certain lines. You can pipe it through something like grep for that. I believe there is a flag to grep to specify lines to ignore (first 23 in your case)

I'll assume you figured it out though since we haven't heard back
 
This worked for me.
:24,$s;^.*/;module=";
:24,$s/$/"/

Here is the explanation of how it works.
:24,$s Substitute from line 24 to end of file.

Most documentation shows / as a separator character in a substitute command. Some other characters also work. Using ; simplifies the command when a / is part of the search string.

s;^.*/;module="; Substitute from beginning (^) of line, looking for a string of any characters (.*) ending with /. Replace with "module="".

s;$;"; Substitute, replacing the end ($) of line with a quotation mark.
 

Members online


Latest posts

Top