Search code examples
regexvimvireplace

What Vim command to use to delete all text after a certain character on every line of a file?


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 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...

For example, 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.

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).


Solution

  • :%s/^\v([^|]+\|[^|]+)\|.*$/\1/