我怎么知道许多txt文件具有的行数和列数 [英] how can I know the number of lines and columns that many txt files have
问题描述
我的目录中有很多文件. 很难一一打开并查看有多少行或有多少列.
I have many files in my directory. It is very difficult to open one by one and see how many lines they have or how many columns they have.
我想知道是否有自动的方法
I want to know if there is any automatic way to do it
作为一个例子.我在桌面上创建一个txt文件,并将其称为文件
As an example. I create a txt file in my desktop and call it my file
check myfile Myname
FALSE 0 Q9Y383
FALSE 1 Q9Y383
FALSE 2 Q9Y383
FALSE 3 Q15366-2
FALSE 6 Q15366-2
FALSE 7 Q15366-2
我将其粘贴到那里,所以我确定我有3列和7行(当我通过xls文件打开它们时)
I paste this in there and so I am sure I have 3 columns and 7 rows (when I open them by xls file)
我尝试对
wc -l mytextfile
它显示0
这只是一个文件,如果我有1000个文件怎么办?
This is only one file, what If I have 1000 files ?
推荐答案
给出:
$ cat /tmp/f.txt
check myfile Myname
FALSE 0 Q9Y383
FALSE 1 Q9Y383
FALSE 2 Q9Y383
FALSE 3 Q15366-2
FALSE 6 Q15366-2
FALSE 7 Q15366-2
对于单个文件,可以使用awk
:
For a single file, you can use awk
:
$ awk 'NR==1{cols=NF} END{print cols, NR}' /tmp/f.txt
3 7
如果拥有gawk
,则可以轻松处理多个文件(*.ext
)文件:
If you have gawk
you can handle multiple files (*.ext
) files easily:
$ gawk 'BEGIN { printf "%4s%8s\n", "cols", "lines"}
FNR==1{cols=NF}
ENDFILE{cnt++;printf "%3i %10i %-60s\n", cols, FNR, FILENAME}
END{ printf "%14i lines in %i files\n", NR, cnt}' /tmp/*.txt
(为我生产)
cols lines
3 7 /tmp/f.txt
1 20000000 /tmp/test.txt
20000007 lines in 2 files
编辑
Edit
如果您拥有古老的Mac文件(其中换行符不是可以使用\n
的某种形式):
If you have ancient Mac files (where the newlines are not some form of \n
) you can do:
$ awk -v RS='\r' 'NR==1{cols=NF} END{print cols, NR}' your_file
或者
$ gawk -v RS='\r' 'BEGIN { printf "%4s%8s\n", "cols", "lines"}
FNR==1 { cols=NF }
ENDFILE { cnt++;printf "%3i %10i %-60s\n", cols, FNR, FILENAME }
END { printf "%14i lines in %i files\n", NR, cnt}' *.files
这篇关于我怎么知道许多txt文件具有的行数和列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!