Given a file with data like this (i.e. stores.dat file)
给定一个包含这样数据的文件(即存储)。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
10 个解决方案
#1
80
awk -F'|' '{print NF; exit}' stores.dat
Just quit right after the first line.
就在第一行之后就辞职。
#2
30
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
#3
9
Unless you're using spaces in there, you should be able to use | wc -w
on the first line.
除非你在使用空格,否则你应该可以在第一行使用| wc -w。
wc
is "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是“单词计数”,它只对输入文件中的单词进行计数。如果只发送一行,它将告诉您列的数量。
#4
4
You could try
你可以试试
cat FILE | awk '{print NF}'
cat文件| awk '{print NF}'
#5
1
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
#6
1
This is usually what I use for counting the number of fields:
这通常是我用来计算字段数的:
head -n 1 file.name | awk -F'|' '{print NF; exit}'
#7
1
Perl solution similar to Mat's awk solution:
类似于Mat awk解决方案的Perl解决方案:
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
#8
0
Based on Cat Kerr response. This command is working on solaris
基于Cat Kerr的反应。这个命令在solaris上工作
awk '{print NF; exit}' stores.dat
#9
0
you may try:
你可以尝试:
head -1 stores.dat | grep -o \| | wc -l
#10
0
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
#1
80
awk -F'|' '{print NF; exit}' stores.dat
Just quit right after the first line.
就在第一行之后就辞职。
#2
30
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
#3
9
Unless you're using spaces in there, you should be able to use | wc -w
on the first line.
除非你在使用空格,否则你应该可以在第一行使用| wc -w。
wc
is "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是“单词计数”,它只对输入文件中的单词进行计数。如果只发送一行,它将告诉您列的数量。
#4
4
You could try
你可以试试
cat FILE | awk '{print NF}'
cat文件| awk '{print NF}'
#5
1
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
#6
1
This is usually what I use for counting the number of fields:
这通常是我用来计算字段数的:
head -n 1 file.name | awk -F'|' '{print NF; exit}'
#7
1
Perl solution similar to Mat's awk solution:
类似于Mat awk解决方案的Perl解决方案:
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
#8
0
Based on Cat Kerr response. This command is working on solaris
基于Cat Kerr的反应。这个命令在solaris上工作
awk '{print NF; exit}' stores.dat
#9
0
you may try:
你可以尝试:
head -1 stores.dat | grep -o \| | wc -l
#10
0
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