bash 计算bash中的列数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5761212/
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
count number of columns in bash
提问by Nick
Say I have a large file with many rows and many columns. I'd like to find out how many rows and columns I have using bash.
假设我有一个包含多行多列的大文件。我想知道我使用 bash 有多少行和列。
回答by Erik
回答by GGibson
Alternatively to count columns, count the separators between columns. I find this to be a good balance of brevity and ease to remember. Of course, this won't work if your data include the column separator.
或者计算列数,计算列之间的分隔符。我发现这是简洁和易于记忆的良好平衡。当然,如果您的数据包含列分隔符,这将不起作用。
head -n1 myfile.txt | grep -o " " | wc -l
Uses head -n1
to grab the first line of the file.
Uses grep -o
to to count all the spaces, and output each space found on a new line. Uses wc -l
to count the number of lines.
用于head -n1
抓取文件的第一行。用于grep -o
计算所有空格,并将找到的每个空格输出到新行。用于wc -l
计算行数。
回答by Fabio
If your file is big but you are certain that the number of columns remains the same for each row (and you have no heading) use:
如果您的文件很大但您确定每行的列数保持不变(并且您没有标题),请使用:
head -n 1 FILE | awk '{print NF}'
to find the number of columns, where FILE is your file name.
查找列数,其中 FILE 是您的文件名。
To find the number of lines 'wc -l FILE' will work.
要查找行数,'wc -l FILE' 将起作用。
回答by FatihSarigol
Little twist to kirill_igum's answer, and you can easily count the number of columns of any certain row you want, which was why I've come to this question, even though the question is asking for the whole file. (Though if your file has same columns in each line this also still works of course):
对 kirill_igum 的回答稍有改动,您可以轻松计算您想要的任何特定行的列数,这就是我提出这个问题的原因,即使该问题要求的是整个文件。(虽然如果您的文件在每一行中都有相同的列,这当然也仍然有效):
head -2 file |tail -1 |tr '\t' '\n' |wc -l
Gives the number of columns of row 2. Replace 2 with 55 for example to get it for row 55.
给出第 2 行的列数。例如用 55 替换 2 以获得第 55 行的列数。
-bash-4.2$ cat file
1 2 3
1 2 3 4
1 2
1 2 3 4 5
-bash-4.2$ head -1 file |tail -1 |tr '\t' '\n' |wc -l
3
-bash-4.2$ head -4 file |tail -1 |tr '\t' '\n' |wc -l
5
Code above works if your file is separated by tabs, as we define it to "tr". If your file has another separator, say commas, you can still count your "columns" using the same trick by simply changing the separator character "t" to ",":
如果您的文件由制表符分隔,则上面的代码有效,因为我们将其定义为“tr”。如果您的文件有另一个分隔符,比如逗号,您仍然可以使用相同的技巧通过简单地将分隔符“t”更改为“,”来计算“列”:
-bash-4.2$ cat csvfile
1,2,3,4
1,2
1,2,3,4,5
-bash-4.2$ head -2 csvfile |tail -1 |tr '\,' '\n' |wc -l
2
回答by bash-o-logist
You can use bash. Note for very large files in terms of GB, use awk/wc
. However it should still be manageable in performance for files with a few MB.
您可以使用 bash。请注意,对于以 GB 为单位的非常大的文件,请使用awk/wc
. 但是,对于几 MB 的文件,它的性能应该仍然可以管理。
declare -i count=0
while read
do
((count++))
done < file
echo "line count: $count"
回答by Tim Sylvester
Simple row count is $(wc -l "$file")
. Use $(wc -lL "$file")
to show both the number of lines and the number of characters in the longest line.
简单的行数是$(wc -l "$file")
. 使用$(wc -lL "$file")
显示的行数和字符中线路最长的号码。
回答by kirill_igum
head -1 file.tsv |head -1 train.tsv |tr '\t' '\n' |wc -l
take the first line, change tabs (or you can use ',' instead of '\t' for commas), count the number of lines.
取第一行,更改制表符(或者您可以使用 ',' 而不是 '\t' 作为逗号),计算行数。
回答by Jelena?uklina
If counting number of columns in the first is enough, try the following:
如果计算第一列的数量就足够了,请尝试以下操作:
awk -F'\t' '{print NF; exit}' myBigFile.tsv
awk -F'\t' '{print NF; exit}' myBigFile.tsv
where \t
is column delimiter.
\t
列分隔符在哪里。
回答by EAdrianH
A very simple way to count the columns of the first line in pure bash (no awk, perl, or other languages):
在纯 bash(无 awk、perl 或其他语言)中计算第一行列的一种非常简单的方法:
read -r line < $input_file
ncols=`echo $line | wc -w`
This will work if your data are formatted appropriately.
如果您的数据格式正确,这将起作用。
回答by aprodan
Following code will do the job and will allow you to specify field delimiter. This is especially useful for files containing more than 20k lines.
以下代码将完成这项工作,并允许您指定字段分隔符。这对于包含超过 20k 行的文件特别有用。
awk 'BEGIN {
FS="|";
min=10000;
}
{
if( NF > max ) max = NF;
if( NF < min ) min = NF;
}
END {
print "Max=" max;
print "Min=" min;
} ' myPipeDelimitedFile.dat