Help with Vim

Discussion in 'General Linux' started by Caute, Mar 27, 2014.

  1. Caute

    Caute New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    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)
  2. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,212
    Likes Received:
    406
    Trophy Points:
    83
  3. Caute

    Caute New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    I appreciate your help but I've been investigating about how to do what it is above and found nothing.
  4. MikeyD

    MikeyD Active Member

    Messages:
    231
    Likes Received:
    109
    Trophy Points:
    43
    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.
  5. MikeyD

    MikeyD Active Member

    Messages:
    231
    Likes Received:
    109
    Trophy Points:
    43
    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
  6. PoconoBread

    PoconoBread New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    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.

Share This Page