Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete numbers at the beginning of lines

Tags:

regex

vim

I have a file "file.txt" in which some rows begins with numbers.

E.g. file.txt:

1 bla bla 390
23 foo foo 100
# bar bar 

some word

45 junk

Is there an easy and fast way to delete the numbers (and the space) from all the lines that start with a number, while deleting only the space from the others?

I would like a command so that the file then looks like:

bla bla 390
foo foo 100
# bar bar 

some word

junk
like image 289
lucacerone Avatar asked Dec 20 '22 20:12

lucacerone


1 Answers

You can use the command editor line:

:%s/^\d*//

This uses the global search %s to find any line that begins with a digit \d* and replace it with nothing //.

Additionally, if you need to remove the extra space after the number as well:

:%s/^\d* //
like image 190
Donovan Avatar answered Jan 10 '23 14:01

Donovan