0

How to delete the line after the first line that contains the pattern invHn3ion? and then move the first line to the end of the file?

Where can I learn all those expressions?

  • 2
    Do you need a script to do this, or do you need instructions of how to do this within vi? – Peschke Sep 24 '15 at 18:06
  • a script will be nice. I just don't know much expressions.i only know how to delete lines matched the pattern in a file:g/pattern/d but how about the lines AFTER the first matched line? and what about move the first line to the end of the file. Totally have no idea how to write cuz i never learn that before. – Wei Chen Sep 24 '15 at 18:14
  • 2
    Maybe the professor doesn't spoon-feed you the answers to the homework because he expects you to be able to figure out how to read the manual. – G-Man Says 'Reinstate Monica' Sep 24 '15 at 18:59
  • he did not give any manual about these stuff – Wei Chen Sep 24 '15 at 19:07
  • there is a wide held belief in the Linux/Unix world that if you teach a man to fish, he will fish for a while, then stop and forget how to fish, but if you throw a man a fishing pole and say "figure it out yourself" they will never forget how to fish for the rest of their life. Anyway, are you looking for a way of doing this inside vi/vim or looking for a way of writing a bash script that will edit a file automatically? You haven't given a lot of information really – Gravy Sep 24 '15 at 21:45
2

Some quicker/simpler keystrokes possible than this, but using my limited vi skills:

  • Move to invHn3ion?

    /invHn3ion?
    
  • Move down a line

    j
    
  • Delete the line

    dd
    
  • Move to first line

    1G
    
  • Delete the line

    dd
    
  • Move to end of file

    G
    
  • Paste

    p
    
  • Save

    ZZ
    
  • 1
    I don't see anything in the question that asks to copy the line containing invHn3ion (and it's hard to tell, but I guess that the ? is part of the question, not part of the pattern).  And, since the question says "move the first line to the end of the file", deleting the first line (1G, dd) and pasting it at the end of the buffer (G, p) seems to be appropriate.  I don't see any reason to do a yank (yy). – G-Man Says 'Reinstate Monica' Sep 24 '15 at 18:56
0

To move the very first line down to the very last line you can try with sed also

e.g.

$ sed -n '1p' file >> file && sed -i '1d' file
0

Let's start with something simpler. In the vi editor, to move the first line to the end, use the ex command m (move):

:1m$

The 1 argument is the source range: just the first line. You can use a range like 2,15m$ or a pattern match /a.*bc/m$. The destination $ denotes the last line of the file. The m command places the moved material after the target position.

There is no line zero, but the target 0 specifies the top of the file. So to move the line back to where it was:

:$m0

I.e. move the last line after the fictitious line zero (thus before line 1).

Okay, so how about moving the first line which matches the pattern invHn3ion? Firstly we have consider that the current position could be anywhere. We want to start the search from the first line, not from the current position, thus:

:1/invHn3ion/m$

What's happening here is that m actually operates on the address argument /invHn3ion/. The 1 is a superfluous address which is evaluated and discarded1. We could get the same effect with two separate commands:

:1
:/invHn3ion/m$

The m command has a sibling t (transfer) which copies rather than moves.


  1. See Addressing in Ex section in the POSIX standard, starting with the paragraph "Commands take zero, one or two addresses ...".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.