Using awk to count number of records

前端 未结 2 525
不知归路
不知归路 2021-01-01 13:14

I want to count all the records in a table through awk but NR prints record nos of all records not the total count. I even tried:

NF==4,count++{print count}
         


        
相关标签:
2条回答
  • 2021-01-01 13:26

    Please show a sample of your file and what you want to do with it (show the desired output ) next time. Just guessing what you want,

    awk 'NF==4{count++} END {print count}' file
    

    the total number of records is indicated by NR.

    awk 'END{print NR}' file1 file2
    

    the total number of records currently is denoted by FNR.

    awk 'END{print FNR}' file
    
    0 讨论(0)
  • 2021-01-01 13:39

    I guess you mean total number of lines. In general, you should use wc -l <filename> for this. If you want to do this using awk, use

    awk '{print NR}' | tail -1
    

    If you only want to use awk, do

    awk 'BEGIN{i=0}{i++;}END{print i}' <filename>
    
    0 讨论(0)
提交回复
热议问题