正则表达式删除每行空格后的字符(Regex remove characters after space in each line)

编程入门 行业动态 更新时间:2024-10-11 17:20:37
正则表达式删除每行空格后的字符(Regex remove characters after space in each line)

我有一个像这样的50k行的文本文件

word1 1 23 word2 43 23 word3 197 word4

并需要一种方法使它看起来像这样:

word1 word2 word3 word4

所以我需要一种方法来删除每行中第一个空格后面的每个字符。 我该怎么做呢?

I have a text file with 50k lines like this

word1 1 23 word2 43 23 word3 197 word4

and need a way to make it look like this:

word1 word2 word3 word4

So I need a way to remove every character behind the first space in each line. How do I do this?

最满意答案

多种解决方案

解决方案1:Vim

在vim中打开文件,然后运行:

:%s/\s.*//g

解决方案2:sed

sed "s/ .*//g" < input_file > output_file

在Windows上不能这样做。

解决方案3:Excel / Calc / Numbers

在OpenOffice / MSOffice / etc中导入文件。 您可以将空间设置为分隔符。 但这是一个更慢,更少的乐趣。 :)

Multiple solutions.

Solution 1: Vim

Open the file in vim, then run:

:%s/\s.*//g

Solution 2: sed

sed "s/ .*//g" < input_file > output_file

Cannot do this on windows tho.

Solution 3: Excel/Calc/Numbers

Import the file in OpenOffice/MSOffice/etc. You can set space as a delimiter. But this is a slower and less fun. :)

更多推荐

本文发布于:2023-08-07 17:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   字符   正则表达式   Regex   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!