简体   繁体   中英

Count number of rows in a text file using linux

I want to know how to get the number of rows in a text file using Linux. I have tried "wc " and "wc -l ", but both display only the number of lines (columns) not the rows. Any idea?

wc -l < <filename> displays lines for me

example file with numbers 1 - 7

outputs:

scottsmudger@ns207588:~ $wc -l < test
7

From the man page:

-l, --lines
    print the newline counts

if you want to get the number of fields in each line you can use awk :

  • awk reads a line from the file and puts it into an internal variable called $0 . Each line is called record. By default, every line is terminated by a newline.
  • Then, every record or line is divided into separate words or fields. Every word is stored in numbered variables $1 , $2 , and so on. There can be as many as 100 fields per record.
  • awk has an internal variable called IFS (Internal Field Separator). IFS is normally whitespace.

In this case we can use an internal built-in variable called NF, which will keep track of field numbers. Typically, the maximum field number will be 100.

awk '{  print NF }' file_name

this command will print the number of columns in each line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM