如何使用vi删除文本文件中每行中的前5个字符?

时间:2022-05-27 22:23:41

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

如何删除文本文件中每行中的前5个字符?我有一个这样的文件:

   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.

我想删除txt文件中每行开头的数字和空格。

5 个解决方案

#1


55  

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

:% s / ^。\{0,5\}/应该这样做。它还处理少于5个字符的情况。

#2


7  

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

使用正则表达式^ .....匹配每行的前5个字符。在全局替换中使用它:

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

#3


3  

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通常是有用的。我用V来选择线条,然后点击:!和使用awk:

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

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

打印数据的第二列。完全节省你的计算空间。

#4


2  

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

因为所有的行都是对齐的,所以不需要用替换来解决这个问题。只需将光标移到左上角(gg),然后按CTRL+vGwlx

#5


1  

Try

试一试

:s/^.....//

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

你可能不需要“^”(线)开始,并会有快捷键5字符,但简单是好的。)

#1


55  

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

:% s / ^。\{0,5\}/应该这样做。它还处理少于5个字符的情况。

#2


7  

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

使用正则表达式^ .....匹配每行的前5个字符。在全局替换中使用它:

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

#3


3  

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通常是有用的。我用V来选择线条,然后点击:!和使用awk:

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

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

打印数据的第二列。完全节省你的计算空间。

#4


2  

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

因为所有的行都是对齐的,所以不需要用替换来解决这个问题。只需将光标移到左上角(gg),然后按CTRL+vGwlx

#5


1  

Try

试一试

:s/^.....//

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

你可能不需要“^”(线)开始,并会有快捷键5字符,但简单是好的。)