What Vim command to use to delete all text after a certain character on every line of a file?
:%s/^\v([^|]+\|[^|]+)\|.*$/\1/
You can also record a macro:
qq02f|Djq
and then you will be able to play it with 100@q
to run the macro on the next 100 lines.
Macro explanation:
qq
: starts macro recording;0
: goes to the first character of the line;2f|
: finds the second occurrence of the|
character on the line;D
: deletes the text after the current position to the end of the line;j
: goes to the next line;q
: ends macro recording.
If you don't have to use Vim, another alternative would be the unix cut
command:
cut -d '|' -f 1-2 file > out.file