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

RegexUnixTextCommandVi

Regex Problem Overview


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.

Regex Solutions


Solution 1 - Regex

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

Solution 2 - Regex

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

:%s/^.....//

Solution 3 - Regex

As all lines are lined up, you don't need a substitution to solve this problem. Just bring the cursor to the top left position (gg), then: CTRL+vGwlx

Solution 4 - Regex

I think easiest way is to use cut.

just type cut -c n- <filename>

Solution 5 - Regex

Try

:s/^.....//

You probably don't need the "^" (start of line), and there'd be shortcuts for the 5 characters - but simple is good :)

Solution 6 - Regex

Since the text looks like it's columnar data, awk would usually be helpful. I'd use V to select the lines, then hit :! and use awk:

:'<,'>! awk '{ print $2 }'

to print out the second column of the data. Saves you from counting spaces altogether.

Solution 7 - Regex

:%s/^.\{0,5\}//g for global, since we want to remove first 5 columns of each line for every line.

Solution 8 - Regex

In my case, to Delete first 2 characters Each Line I used this :%s/^.\{0,2\}// and it works with or without g the same.

I am on a VIM - Vi IMproved 8.2, macOS version, Normal version without GUI.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmmyView Question on Stackoverflow
Solution 1 - RegexlaredView Answer on Stackoverflow
Solution 2 - RegexBarmarView Answer on Stackoverflow
Solution 3 - RegexMarkView Answer on Stackoverflow
Solution 4 - RegexBedi EgilmezView Answer on Stackoverflow
Solution 5 - RegexracramanView Answer on Stackoverflow
Solution 6 - RegexeasoncxzView Answer on Stackoverflow
Solution 7 - Regexuser3242313View Answer on Stackoverflow
Solution 8 - RegexAx_View Answer on Stackoverflow