How would I create a VIM or Vi command to delete all text after a certain character for every line i

Posted by Jason Down on Stack Overflow See other posts from Stack Overflow or by Jason Down
Published on 2009-01-15T20:30:34Z Indexed on 2010/03/27 13:33 UTC
Read the original article Hit count: 206

Filed under:
|
|
|

Scenario:

  • I have a text file that has pipe (as in the "|" character) delimited data.
  • Each field of data in the pipe delimited fields can be of variable length, so counting characters won't work (or using some sort of substring function... if that even exists in VIM).

Is it possible, using VIM / Vi to delete all data from the second pipe to the end of the line for the entire file? There are approx 150,000 lines, so doing this manually would only be appealing to a masochist...

e.g.

Change the following lines from:

1111|random sized text 12345|more random data la la la|1111|abcde

2222|random sized text abcdefghijk|la la la la|2222|defgh

3333|random sized text|more random data|33333|ijklmnop

to:

1111|random sized text 12345

2222|random sized text abcdefghijk

3333|random sized text

I'm sure this can be done somehow... I hope.

TIA

UPDATE: I should have mentioned that I'm running this on Windows XP, so I don't have access to some of the mentioned *nix commands (CUT is not recognized on Windows).

© Stack Overflow or respective owner

Related posts about vi

Related posts about vim