how can I know the number of lines and columns tha

2019-09-12 00:49发布

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

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

I paste this in there and so I am sure I have 3 columns and 7 rows (when I open them by xls file)

I tried to do it for one single file like

wc -l mytextfile

it shows 0

This is only one file, what If I have 1000 files ?

4条回答
女痞
2楼-- · 2019-09-12 01:01

Your file has ‘mac’ line endings – that is, lines separated by carriage-return rather than newline (which are ‘unix’ line endings), and it appears that wc can recognise only the latter.

You have two options: convert your input files to ‘mac’ line endings once, or on the fly.

For example

% alias frommac="tr '\r' '\n'"
% frommac <myfile >myfile.unix
% wc -l myfile.unix

or

% frommac <myfile | wc -l

If you have lots of these files, then you could do something like

% wc -l *.unix

(if you've pre-converted the input files as above), or

% for f in *; do frommac <$f | wc -l; done

...or something along those lines.

查看更多
虎瘦雄心在
3楼-- · 2019-09-12 01:03

Just use for statement.

for f in *
do
    wc -l "$f"
done

and add things to for loop, when you have any other things to repeat

查看更多
爷的心禁止访问
4楼-- · 2019-09-12 01:22

wc -l file will show you number of lines; assuming comma-separated values and no literal commas in the header, read -r -d $'\r' -a cols <file && echo "${#cols[@]}" will give you number of columns (in the first line).

All of these will work with wildcards. If you have 1000 files, then, you can run:

printf '%s\0' *.txt | xargs -0 wc -l

...or...

for file in *.txt; do
  read -r -a cols <"$file" && echo "$file ${#cols[@]}"
done

Note that in at least one other question, you had a text file with CR newlines rather than LF or CRLF newlines. For those, you'll want to use read -r -d $'\r' -a cols.

Similarly, if your text file format prevents wc -l from working correctly for that same reason, you might need the following much-less-efficient alternative:

for file in *.txt; do
  printf '%s\t' "$file"
  tr '\r' '\n' <"$file" | wc -l
done
查看更多
戒情不戒烟
5楼-- · 2019-09-12 01:26

Given:

$ 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

For a single file, you can use awk:

$ awk 'NR==1{cols=NF} END{print cols, NR}' /tmp/f.txt
3 7

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

Which produces (for me)

cols   lines
  3          7 /tmp/f.txt                                                  
  1   20000000 /tmp/test.txt                                               
      20000007 lines in 2 files

Edit

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

Or,

 $ 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
查看更多
登录 后发表回答