Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove first 5 characters in each line in a text file using vi?

How do I remove the first 5 characters in each line in a text file?
I have a file like this:

   4 Alabama    4 Alaska    4 Arizona    4 Arkansas    4 California   54 Can    8 Carolina    4 Colorado    4 Connecticut    8 Dakota    4 Delaware   97 Do    4 Florida    4 Hampshire   47 Have    4 Hawaii 

I'd like to remove the number and the space at the beginning of each line in my txt file.

like image 545
Emmy Avatar asked Feb 10 '15 00:02

Emmy


People also ask

How do I remove the first character from every line?

Click and hold down mouse button and select the first character of every line, and release. Press the DELETE key.

How do you delete a character in Vi?

To delete one character, position the cursor over the character to be deleted and type x . The x command also deletes the space the character occupied—when a letter is removed from the middle of a word, the remaining letters will close up, leaving no gap. You can also delete blank spaces in a line with the x command.


2 Answers

:%s/^.\{0,5\}// should do the trick. It also handles cases where there are less than 5 characters.

like image 74
lared Avatar answered Sep 28 '22 07:09

lared


Use the regular expression ^..... to match the first 5 characters of each line. use it in a global substitution:

:%s/^.....// 
like image 26
Barmar Avatar answered Sep 28 '22 07:09

Barmar