Linux unix - 文件中的列数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8629330/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:48:42  来源:igfitidea点击:

unix - count of columns in file

linuxbashshellunixscripting

提问by toop

Given a file with data like this (i.e. stores.dat file)

给定一个包含这样数据的文件(即 stores.dat 文件)

sid|storeNo|latitude|longitude
2|1|-28.03720000|153.42921670
9|2|-33.85090000|151.03274200

What would be a command to output the number of column names?

输出列名数量的命令是什么?

i.e. In the example above it would be 4. (number of pipe characters + 1 in the first line)

即在上面的示例中,它将是 4。(管道字符数 + 第一行中的 1)

I was thinking something like:

我在想这样的事情:

awk '{ FS = "|" } ; { print NF}' stores.dat

but it returns all lines instead of just the first and for the first line it returns 1 instead of 4

但它返回所有行而不是第一行,对于第一行,它返回 1 而不是 4

采纳答案by Mat

awk -F'|' '{print NF; exit}' stores.dat 

Just quit right after the first line.

在第一行之后立即退出。

回答by Tom van der Woerdt

Unless you're using spaces in there, you should be able to use | wc -won the first line.

除非您在其中使用空格,否则您应该可以| wc -w在第一行使用。

wcis "Word Count", which simply counts the words in the input file. If you send only one line, it'll tell you the amount of columns.

wc是“Word Count”,它只是计算输入文件中的单词。如果您只发送一行,它会告诉您列的数量。

回答by miku

This is a workaround (for me: I don't use awk very often):

这是一种解决方法(对我来说:我不经常使用 awk):

Display the first row of the file containing the data, replace all pipes with newlines and then count the lines:

显示包含数据的文件的第一行,用换行符替换所有管道,然后计算行数:

$ head -1 stores.dat | tr '|' '\n' | wc -l

回答by Don Question

If you have python installed you could try:

如果你安装了 python,你可以尝试:

python -c 'import sys;f=open(sys.argv[1]);print len(f.readline().split("|"))' \
    stores.dat

回答by user2533399

This is usually what I use for counting the number of fields:

这通常是我用来计算字段数的方法:

head -n 1 file.name | awk -F'|' '{print NF; exit}'

回答by Cat Kerr

You could try

你可以试试

cat FILE | awk '{print NF}'

猫文件| awk '{打印 NF}'

回答by Chris Koknat

Perl solution similar to Mat's awk solution:

Perl 解决方案类似于 Mat 的 awk 解决方案:

perl -F'\|' -lane 'print $#F+1; exit' stores.dat

I've tested this on a file with 1000000 columns.

我已经在一个包含 1000000 列的文件上对此进行了测试。



If the field separator is whitespace (one or more spaces or tabs) instead of a pipe:

如果字段分隔符是空格(一个或多个空格或制表符)而不是管道:

perl -lane 'print $#F+1; exit' stores.dat

回答by Manu Mitra

Based on Cat Kerr response. This command is working on solaris

基于 Cat Kerr 的响应。此命令适用于solaris

awk '{print NF; exit}' stores.dat

回答by user7231103

you may try:

你可以尝试:

head -1 stores.dat | grep -o \|  | wc -l

回答by sAguinaga

select any row in the file (in the example below, it's the 2nd row) and count the number of columns, where the delimiter is a space:

选择文件中的任何行(在下面的示例中,它是第二行)并计算列数,其中分隔符是一个空格:

sed -n 2p text_file.dat | tr ' ' '\n' | wc -l