如何从 tsv 文件中提取第一列?
How to extract the first column from a tsv file?
我有一个包含一些数据的文件,我只想将第一列用作脚本的标准输入,但我无法提取它。
我试过用这个
awk -F"\t" '{print }' inputs.tsv
但它只显示第一列的第一个字母。我尝试了一些其他的东西,但它要么显示整个文件,要么只显示第一列的第一个字母。
我的文件看起来像这样:
Harry_Potter 1
Lord_of_the_rings 10
Shameless 23
....
试试这个(最好依赖真正的 csv 解析器...):
csvcut -c 1 -f $'\t' file
勾选csvkit
输出:
Harry_Potter
Lord_of_the_rings
Shameless
注:
正如@RomanPerekhrest 所说,您应该修复损坏的样本输入(我们看到了需要制表符的空格...)
您可以使用在所有 Unix 和 Linux 系统上可用的 cut
:
cut -f1 inputs.tsv
您不需要指定 -d
选项,因为制表符是默认分隔符。来自 man cut
:
-d delim
Use delim as the field delimiter character instead of the tab character.
正如本杰明所说,您的 awk
命令确实正确。 Shell 将文字 \t 作为参数传递,awk 会将其解释为制表符,而 cut
等其他命令可能不会。
不确定为什么你只得到第一个字符作为输出。
你可能想看看这个post:
- Difference between single and double quotes in Bash
我有一个包含一些数据的文件,我只想将第一列用作脚本的标准输入,但我无法提取它。 我试过用这个
awk -F"\t" '{print }' inputs.tsv
但它只显示第一列的第一个字母。我尝试了一些其他的东西,但它要么显示整个文件,要么只显示第一列的第一个字母。
我的文件看起来像这样:
Harry_Potter 1
Lord_of_the_rings 10
Shameless 23
....
试试这个(最好依赖真正的 csv 解析器...):
csvcut -c 1 -f $'\t' file
勾选csvkit
输出:
Harry_Potter
Lord_of_the_rings
Shameless
注:
正如@RomanPerekhrest 所说,您应该修复损坏的样本输入(我们看到了需要制表符的空格...)
您可以使用在所有 Unix 和 Linux 系统上可用的 cut
:
cut -f1 inputs.tsv
您不需要指定 -d
选项,因为制表符是默认分隔符。来自 man cut
:
-d delim Use delim as the field delimiter character instead of the tab character.
正如本杰明所说,您的 awk
命令确实正确。 Shell 将文字 \t 作为参数传递,awk 会将其解释为制表符,而 cut
等其他命令可能不会。
不确定为什么你只得到第一个字符作为输出。
你可能想看看这个post:
- Difference between single and double quotes in Bash