如何使用 linux 命令显示文件中每一行的第一个单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15434728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display the first word of each line in my file using the linux commands?
提问by MOHAMED
I have a file containing many lines, and I want to display only the first word of each line with the Linux commands.
我有一个包含多行的文件,我只想用 Linux 命令显示每行的第一个单词。
How can I do that?
我怎样才能做到这一点?
回答by newfurniturey
You can use awk
:
您可以使用awk
:
awk '{print }' your_file
This will "print" the first column ($1
) in your_file
.
这将“打印”中的第一列 ( $1
) your_file
。
回答by Memento Mori
I see there are already answers. But you can also do this with sed:
我看到已经有答案了。但是你也可以用 sed 做到这一点:
sed 's/ .*//' fileName
回答by Gilles Quenot
try doing this with coreutils cut
:
尝试这样做coreutils cut
:
cut -d' ' -f1 file
回答by Jeffrey Froman
The above solutions seem to fit your specific case. For a more general application of your question, consider that words are generally defined as being separated by whitespace, but not necessarily space characters specifically. Columns in your file may be tab-separated, for example, or even separated by a mixture of tabs and spaces.
上述解决方案似乎适合您的具体情况。对于您的问题的更一般应用,请考虑将单词通常定义为由空格分隔,但不一定是专门的空格字符。例如,文件中的列可能以制表符分隔,甚至可以由制表符和空格的混合分隔。
The previous examples are all useful for finding space-separated words, while only the awk example also finds words separated by other whitespace characters (and in fact this turns out to be rather difficult to do uniformly across various sed/grep versions). You may also want to explicitly skip empty lines, by amending the awk statement thus:
前面的示例对于查找空格分隔的单词都很有用,而只有 awk 示例还可以查找由其他空格字符分隔的单词(事实上,这在各种 sed/grep 版本中很难统一执行)。您可能还想通过修改 awk 语句来显式跳过空行:
awk '{if ( !="") print }' your_file
If you are also concerned about the possibility of empty fields, i.e., lines that beginwith whitespace, then a more robust solution would be in order. I'm not adept enough with awk to produce a one-liner for such cases, but a short python script that does the trick might look like:
如果您还担心出现空字段(即以空格开头的行)的可能性,那么可能需要更强大的解决方案。我对 awk 不够熟练,无法为这种情况生成单行代码,但是执行此操作的简短 python 脚本可能如下所示:
>>> import re
>>> for line in open('your_file'):
... words = re.split(r'\s', line)
... if words and words[0]:
... print words[0]
回答by user3038458
...or on Windows (if you have GnuWin32 grep) :
...或在 Windows 上(如果您有 GnuWin32 grep):
grep -Eo "^[^ ]+" file