Lucas
Lucas

Reputation: 457

Delete all characters after "." in each line

I have a text file with about 2,000 lines of text and I want to remove a large portion of each line of text.

The text is in this format:

Important Text that I care about. Stuff I want to delete

I am unsure as to how to delete all of the text after the . in each line.

Can someone give me a quick command that would do this?

Upvotes: 34

Views: 60162

Answers (5)

Patrick Bacon
Patrick Bacon

Reputation: 4660

Use the Substitution Ex Command to Trim All Lines

This is very similar to both answers, yet I think there is value in presenting it.

Like the other answers, I just used the ex substitution command:

 :%s/[^.]*$//

Explanation of substitution:

% indicates a range for all lines.

[^.] is a character class of all non-period characters

* is a quantifier indicating 0 or more matches.

$ is an anchor which communicates to VIM that we want this pattern to match at the end of the line.


Addendum

The solution assumes each line will have a period, otherwise the command will not work as expected as @Qeole has indicated. Qeole's solution addresses non-periods lines appropriately.

Upvotes: 11

dvk317960
dvk317960

Reputation: 802

with the cursor at the first character of first line.

fS<Ctrl-V>G$d

Upvotes: 1

romainl
romainl

Reputation: 196886

Various additional :normal solutions:

:%norm )Dx
:%norm $T.D
:%norm f.C.
:%norm 0/\. /e<C-v><CR>D

:normal

Upvotes: 31

Qeole
Qeole

Reputation: 9184

With substitutions:

:%s/\..*/./

With :normal command:

:%norm f.lD

Upvotes: 46

antogerva
antogerva

Reputation: 559

Use search and replace "vim feature" combined with regex: :%s/\..*$//g

Upvotes: 2

Related Questions