Help with Vi - Unix

This is a discussion on Help with Vi - Unix ; Hi all, Could someone help me with executing an macro n-times in VI? This is what i want to do, I want to add the text "12345" at the beginning of every second line of a text file. So I ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: Help with Vi

  1. Help with Vi

    Hi all,

    Could someone help me with executing an macro n-times in VI? This is
    what i want to do, I want to add the text "12345" at the beginning of
    every second line of a text file.

    So I did this, :map q i12345 2j0 . However, when i pressed n-q
    the only thing that was repeated by n-times was the text insertation of
    12345, the line down and 0-columns were not repeated (n is a number).

    What should I do to fix this? Thank!


  2. Re: Help with Vi

    lorak88@gmail.com wrote:
    > Hi all,
    >
    > Could someone help me with executing an macro n-times in VI? This is
    > what i want to do, I want to add the text "12345" at the beginning of
    > every second line of a text file.
    >
    > So I did this, :map q i12345 2j0 . However, when i pressed n-q
    > the only thing that was repeated by n-times was the text insertation of
    > 12345, the line down and 0-columns were not repeated (n is a number).


    Hi,

    normally I would run it once and repeat it with @q, so the sequence
    would be:

    qq
    i12345 0
    q

    than to execute:

    N
    @q

    note that the enters are just to illustrate blocks, you don't need to
    type them as well. 'q' is to "start recording" and 'q' again to stop it.
    The second 'q' is the macro name (the same you used on your 'map').

    --renato

  3. Re: Help with Vi

    Renato Golin wrote:
    > i12345 0


    Sorry, forgot the ESC... it would be:

    i12345

    or

    i12345 2j0

    if you prefer...

    --renato

  4. Re: Help with Vi

    On 21 Sep 2006 02:24:42 -0700, lorak88@gmail.com wrote:
    > Hi all,
    >
    > Could someone help me with executing an macro n-times in VI? This is
    > what i want to do, I want to add the text "12345" at the beginning of
    > every second line of a text file.
    >
    > So I did this, :map q i12345 2j0 . However, when i pressed n-q
    > the only thing that was repeated by n-times was the text insertation of
    > 12345, the line down and 0-columns were not repeated (n is a number).

    [...]

    Might be as easy to do:

    %!sed 'n;s/^/12345/'

    --
    Stephane

  5. Re: Help with Vi

    Stephane Chazelas wrote:
    > Might be as easy to do:
    >
    > %!sed 'n;s/^/12345/'


    But that would change all lines and not every two lines. Maybe stating
    if N%2 before changing (I guess sed can do that, just don't know how)
    would work?

    --renato

  6. Re: Help with Vi

    > Hi,
    >
    > normally I would run it once and repeat it with @q, so the sequence
    > would be:
    >
    > qq
    > i12345 0
    > q
    >
    > than to execute:
    >
    > N
    > @q
    >
    > note that the enters are just to illustrate blocks, you don't need to
    > type them as well. 'q' is to "start recording" and 'q' again to stop it.
    > The second 'q' is the macro name (the same you used on your 'map').
    >
    > --renato


    thanks for the quick response. After I tried out the sugguestion as
    indicated by renato i still have the same problem. pressing "@q" once
    did what i expected it to. However, when i tried to do the n-times
    repetition (by using the numeric precursor to the "@q" command) it
    failed.

    vi executed N-times "text insertion" follow by one single move instead
    of n-times "text insertion and move"

    any sugguestion?

    thank you


  7. Re: Help with Vi

    Renato Golin wrote:
    > Stephane Chazelas wrote:
    >> Might be as easy to do:
    >>
    >> %!sed 'n;s/^/12345/'

    >
    > But that would change all lines and not every two lines.


    If it would change all lines, then how come it only changes
    every other line when I run it?

    - Logan

  8. Re: Help with Vi

    lorak88@gmail.com wrote:
    > vi executed N-times "text insertion" follow by one single move instead
    > of n-times "text insertion and move"


    which version of vi are you using? Movement works for me on macros since
    version 5...

    what I understood you just need to insert a number at the beginning of
    every other line, is that all?

    if it's that so you can also use perl, awk or sed...

    cheers,
    --renato

  9. Re: Help with Vi

    Logan Shaw wrote:
    > Renato Golin wrote:
    >> Stephane Chazelas wrote:
    >>> Might be as easy to do:
    >>>
    >>> %!sed 'n;s/^/12345/'

    >>
    >> But that would change all lines and not every two lines.

    >
    > If it would change all lines, then how come it only changes
    > every other line when I run it?


    Leprechauns, they always do that, you know... but serious, it was meant
    to be a question, I'm not a sed expert, perl made me lazy about it (I
    know it's not right).

    Now for the real question: your regexp substitute head of line for 12345
    'n' times, right? Where's the instruction to jump every other line?

    --renato

  10. Re: Help with Vi

    > which version of vi are you using? Movement works for me on macros since
    > version 5...
    >
    > what I understood you just need to insert a number at the beginning of
    > every other line, is that all?
    >
    > if it's that so you can also use perl, awk or sed...
    >
    > cheers,
    > --renato


    I'm using SVR4.0, Solaris 2.5.

    Actually what i want to do is general format editing, I used the
    insertion of number as an example.

    Thanks!


  11. Re: Help with Vi

    2006-09-25, 10:31(+01), Renato Golin:
    > Logan Shaw wrote:
    >> Renato Golin wrote:
    >>> Stephane Chazelas wrote:
    >>>> Might be as easy to do:
    >>>>
    >>>> %!sed 'n;s/^/12345/'
    >>>
    >>> But that would change all lines and not every two lines.

    >>
    >> If it would change all lines, then how come it only changes
    >> every other line when I run it?

    >
    > Leprechauns, they always do that, you know... but serious, it was meant
    > to be a question, I'm not a sed expert, perl made me lazy about it (I
    > know it's not right).
    >
    > Now for the real question: your regexp substitute head of line for 12345
    > 'n' times, right? Where's the instruction to jump every other line?

    [...]

    n is the instruction to replace the pattern space with the next
    line of input.


    --
    Stéphane

  12. Re: Help with Vi

    Stephane CHAZELAS wrote:
    > n is the instruction to replace the pattern space with the next
    > line of input.


    Got it now: next, replace, next, replace...

    and you can limit the amount of times you'll run that by calling:

    ..,N!sed(...)

    instead of

    %!sed(...)

    being N the number of lines (or times-to-replace*2)

    thanks!!
    --renato

  13. Re: Help with Vi

    lorak88@gmail.com wrote:
    >> which version of vi are you using? Movement works for me on macros since
    >> version 5...

    >
    > I'm using SVR4.0, Solaris 2.5.
    >
    > Actually what i want to do is general format editing, I used the
    > insertion of number as an example.


    I see, the sed solution stephane came out would be simpler if the macro
    didn't worked as you'd like. Did you tried?

    cheers,
    --renato

+ Reply to Thread