So I'm trying to print the first word in each line of a txt file. The words are separated by one blank.
所以我试图在txt文件的每一行打印第一个单词。单词用一个空格分隔。
cut -c 1 txt file
Thats the code I have so far but it only prints the first character of each line. Thanks
这是我到目前为止的代码,但它只打印每行的第一个字符。谢谢
5 个解决方案
#1
23
To print a whole word, you want -f 1
, not -c 1
. And since the default field delimiter is TAB rather than SPACE, you need to use the -d
option.
要打印整个单词,您需要-f 1,而不是-c 1.并且由于默认字段分隔符是TAB而不是SPACE,因此您需要使用-d选项。
cut -d' ' -f1 filename
To print the last two words not possible with cut
, AFAIK, because it can only count from the beginning of the line. Use awk
instead:
要打印最后两个单词不能用cut,AFAIK,因为它只能从行的开头算起。请改用awk:
awk '{print $(NF-1), $NF;}' filename
#2
17
you can try
你可以试试
awk '{print $1}' your_file
#3
10
read word _ < file
echo "$word"
What's nice about this solution is it doesn't read beyond the first line of the file. Even awk, which has some very clean, terse syntax, has to be explicitly told to stop reading past the first line. read
just reads one line at a time. Plus it's a bash builtin (and a builtin in many shells), so you don't need a new process to run.
这个解决方案有什么好处,它不会超出文件的第一行。即使是具有一些非常干净,简洁的语法的awk,也必须明确地告知要停止阅读第一行。读取只读取一行一行。另外,它是一个内置的bash(以及内置于许多shell中),因此您不需要运行新进程。
If you want to print the first word in each line:
如果要打印每行中的第一个单词:
while read word _; do printf '%s\n' "$word"; done < file
But if the file is large then awk or cut will win out for reading every line.
但是如果文件很大,那么awk或cut会赢得读取每一行。
#4
1
-c
is for characters, you want -f
for fields, and -d
to indicate your separator of space instead of the default tab:
-c表示字符,您希望-f表示字段,-d表示空格分隔符而不是默认选项卡:
cut -d " " -f 1 file
#5
-1
You can use:
您可以使用:
cut -d\ -f1 file
Where:
哪里:
- -d is the delimiter (here using
\
for a space) - -d是分隔符(这里使用\作为空格)
- -f is the field selector
- -f是字段选择器
Notice that there is a space
after the \
.
请注意\后面有一个空格。
#1
23
To print a whole word, you want -f 1
, not -c 1
. And since the default field delimiter is TAB rather than SPACE, you need to use the -d
option.
要打印整个单词,您需要-f 1,而不是-c 1.并且由于默认字段分隔符是TAB而不是SPACE,因此您需要使用-d选项。
cut -d' ' -f1 filename
To print the last two words not possible with cut
, AFAIK, because it can only count from the beginning of the line. Use awk
instead:
要打印最后两个单词不能用cut,AFAIK,因为它只能从行的开头算起。请改用awk:
awk '{print $(NF-1), $NF;}' filename
#2
17
you can try
你可以试试
awk '{print $1}' your_file
#3
10
read word _ < file
echo "$word"
What's nice about this solution is it doesn't read beyond the first line of the file. Even awk, which has some very clean, terse syntax, has to be explicitly told to stop reading past the first line. read
just reads one line at a time. Plus it's a bash builtin (and a builtin in many shells), so you don't need a new process to run.
这个解决方案有什么好处,它不会超出文件的第一行。即使是具有一些非常干净,简洁的语法的awk,也必须明确地告知要停止阅读第一行。读取只读取一行一行。另外,它是一个内置的bash(以及内置于许多shell中),因此您不需要运行新进程。
If you want to print the first word in each line:
如果要打印每行中的第一个单词:
while read word _; do printf '%s\n' "$word"; done < file
But if the file is large then awk or cut will win out for reading every line.
但是如果文件很大,那么awk或cut会赢得读取每一行。
#4
1
-c
is for characters, you want -f
for fields, and -d
to indicate your separator of space instead of the default tab:
-c表示字符,您希望-f表示字段,-d表示空格分隔符而不是默认选项卡:
cut -d " " -f 1 file
#5
-1
You can use:
您可以使用:
cut -d\ -f1 file
Where:
哪里:
- -d is the delimiter (here using
\
for a space) - -d是分隔符(这里使用\作为空格)
- -f is the field selector
- -f是字段选择器
Notice that there is a space
after the \
.
请注意\后面有一个空格。