There are multiple ways to count the number of lines of a file in Linux. In our daily life we need to count number of lines of a csv file, text file etc and the most popular command we use "wc -l". In this article i will show you 5 different ways to find number of line along with wc -l
Let us consider we have 2 sample file name as sample1.txt and sample2.txt which having 10 and 3 lines as following. I will show you different examples to get the number of lines.
Contents of sample1.txt
Contents of sample2.txt
The wc (word count) command is very popular in Unix/Linux to find number of lines count, word counts, byte and characters count in a file. The syntax of wc command is as following.
In the above example first column shows number of line , 2nd column shows words and 3rd column shows number of chars. Last row shows the total counts of all files.
Lets us have a look of different ways to find the number of lines in a file , we will use sample1.txt in our demo.
1) Counting lines with WC command
2) Counting lines with sed command
sed uses "=" operator for line numbering and "$" gives the last count of numbering which is total number of lines.
3) grep command with -c option:
To count all non-empty lines or non-blank line.
4). Counting line with awk:
awk with NR variable gives the line numbers and by printing NR with the end block it gives the line number of the last line which is nothing but a total number of lines in file.
5). Counting lines with perl:
End block as in awk we can use also with perl. In Perl "$." gives the number of lines.
Note:
To find the longest line character count we can use wc with -L option. As shown below in sample1.txt file the longest line having 24 character where as in sample2.txt it is 16.
Have a look on couple of heck of counting lines.
Trick 01:
Explanation : "cat -n sample1.txt" keeps a number line in file and pipe "tail -n1" gives the first row from bottom which includes line number and last line content. Then pipe "cut -f1" take out the first field of last line(last line number) which is nothing but the total count of lines.
Trick 02:
Using the while loop reading file line by line and increasing counter.
Let us consider we have 2 sample file name as sample1.txt and sample2.txt which having 10 and 3 lines as following. I will show you different examples to get the number of lines.
Contents of sample1.txt
[~]$ cat sample1.txt
One
Two
Three
four
five
six This is longest line
seven
eight
nine
ten
One
Two
Three
four
five
six This is longest line
seven
eight
nine
ten
Contents of sample2.txt
[~]$ cat sample2.txt
One
Two Longest Line
Three
One
Two Longest Line
Three
The wc (word count) command is very popular in Unix/Linux to find number of lines count, word counts, byte and characters count in a file. The syntax of wc command is as following.
wc [OPTION]... [FILE]...
Where OPTION are as below:
-c, --bytes print the byte counts
-m, --chars print the character counts
-l, --lines print the newline counts
--files0-from=F read input from the files specified by
NUL-terminated names in file F;
If F is - then read names from standard input
-L, --max-line-length print the length of the longest line
-w, --words print the word counts
Where OPTION are as below:
-c, --bytes print the byte counts
-m, --chars print the character counts
-l, --lines print the newline counts
--files0-from=F read input from the files specified by
NUL-terminated names in file F;
If F is - then read names from standard input
-L, --max-line-length print the length of the longest line
-w, --words print the word counts
[~]$ wc sample1.txt sample2.txt
10 14 70 sample1.txt
3 5 27 sample2.txt
13 19 97 total
10 14 70 sample1.txt
3 5 27 sample2.txt
13 19 97 total
In the above example first column shows number of line , 2nd column shows words and 3rd column shows number of chars. Last row shows the total counts of all files.
Lets us have a look of different ways to find the number of lines in a file , we will use sample1.txt in our demo.
1) Counting lines with WC command
[~]$ cat sample1.txt | wc -l
10
or10
[~]$ wc -l sample1.txt
10 sample1.txt
10 sample1.txt
2) Counting lines with sed command
sed uses "=" operator for line numbering and "$" gives the last count of numbering which is total number of lines.
[~]$ sed -n "$=" sample1.txt
10
10
3) grep command with -c option:
To count all non-empty lines or non-blank line.
[~]$ grep -c "." sample2.txt
3
To count all line including blank or empty lines.3
[~]$ grep -c ".*" sample2.txt
4
or4
[~]$ grep -c "^" sample2.txt
4
4
4). Counting line with awk:
awk with NR variable gives the line numbers and by printing NR with the end block it gives the line number of the last line which is nothing but a total number of lines in file.
[~]$ awk 'END {print NR}' sample1.txt
10
10
5). Counting lines with perl:
End block as in awk we can use also with perl. In Perl "$." gives the number of lines.
[~]$ perl -lne 'END {print $.}' sample1.txt
10
10
Note:
To find the longest line character count we can use wc with -L option. As shown below in sample1.txt file the longest line having 24 character where as in sample2.txt it is 16.
[~]$ wc -L sample1.txt sample2.txt
24 sample1.txt
16 sample2.txt
24 total
24 sample1.txt
16 sample2.txt
24 total
Have a look on couple of heck of counting lines.
Trick 01:
cat -n sample1.txt | tail -n 1 | cut -f1
Explanation : "cat -n sample1.txt" keeps a number line in file and pipe "tail -n1" gives the first row from bottom which includes line number and last line content. Then pipe "cut -f1" take out the first field of last line(last line number) which is nothing but the total count of lines.
Trick 02:
Using the while loop reading file line by line and increasing counter.
#!/bin/bash
count=0
while read
do
((count=$count+1))
done <sample1.txt
echo $count
count=0
while read
do
((count=$count+1))
done <sample1.txt
echo $count
***End***
No comments:
Post a Comment