How to count the number of lines of file in Linux

In the process of log analysis, we often encounter the situation of counting file lines. It can help us analyze business data.

This linux tutorial mainly shares with you the use of linux commands to count file lines, the number of repeated lines, the number of unique lines, and the number of occurrences of specific content.

Through the use of these linux commands, improve the efficiency of daily text processing.

So how to count the number of lines of file in Linux?

Count the total lines number of files

using linux wc command

➜ wc -l test.txt

using linux pipe, cat and wc commands

➜ cat test.txt | wc -l

using linux awk command

➜ awk 'END{print NR}' test.txt

# OR

➜ awk '{print NR}' test.txt | tail -n1

Count the total number of non repeating lines

➜ cat test.txt | sort| uniq | wc -l

Count the total number of duplicate lines

# Sort and count the number of repetitions per row
➜ sort test.log | uniq -c

# Number of lines with output repetition greater than 1
➜ sort test.log | uniq -c  | awk -F' ' '{if($1 > 1) { print $0 }}'

# Count total number of duplicate lines
➜ sort test.log | uniq -c  | awk -F' ' '{if($1 > 1) { print $0 }}' | wc -l
   

Count the number of times a specific content appears in a file

➜ grep -c 'awk' test.log
  • -c, –count
    Only a count of selected lines is written to standard output.
➜ grep 'awk' test.log | wc -l

Add a Comment

Your email address will not be published.