问题描述
我有一个具有以下格式的文本文件:
I have a text file that has the following format:
characters(that I want to keep) (space) characters(that I want to remove)
例如:
foo garbagetext
hello moregarbage
keepthis removethis
(etc.)
因此,我试图在Linux中使用grep命令,以使每行中的字符最多保持不超过第一个空格.我尝试了许多尝试,例如:
So I was trying to use the grep command in Linux to keep only the characters in each line up to and not including the first blank space. I have tried numerous attempts such as:
grep '*[[:space:]]' text1.txt > text2.txt
grep '*[^\s]' text1.txt > text2.txt
grep '/^[^[[:space:]]]+/' text1.txt > text2.txt
试图将不同的例子拼凑在一起,但是我没有运气.它们都产生一个空白的text2.txt
文件.我是新来的.我在做什么错了?
trying to piece together from different examples, but I have had no luck. They all produce a blank text2.txt
file. I am new to this. What am I doing wrong?
*
我要保留的部分包括大写字母.因此,我想保留任何/所有字符,并且不包括每行中的空格(从空格开始删除所有内容).
The parts I want to keep include capital letters. So I want to keep any/all characters up to and not including the blank space (removing everything from the blank space onward) in each line.
**
(我要删除的)垃圾文本可以包含任何内容,包括空格,特殊字符等.因此,例如:
The garbage text (that I want to remove) can contain anything, including spaces, special characters, etc. So for example:
AA rough, cindery lava [n -S]
运行grep -o '[^ ]*' text1.txt > text2.txt
后,上面的行变为:
After running grep -o '[^ ]*' text1.txt > text2.txt
, the line above becomes:
AA
rough,
cindery
lava
[n
-S]
在text2.txt中. (我只想保留AA
)
in text2.txt. (All I want to keep is AA
)
解决方案(由Rohit Jain提供,并由beny23进一步输入):
SOLUTION (provided by Rohit Jain with further input by beny23):
grep -o '^[^ ]*' text1.txt > text2.txt
推荐答案
您将量词*
放在错误的位置.
You are putting quantifier *
at the wrong place.
请尝试以下方法:-
grep '^[^\s]*' text1.txt > text2.txt
或者甚至更好:-
grep '^\S*' text1.txt > text2.txt
\S
表示匹配非空白字符.锚点^
用于在行的开头进行匹配.
\S
means match non-whitespace character. And anchor ^
is used to match at the beginning of the line.
这篇关于grep:匹配所有字符,直到(不包括)第一个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!