Alvin
Alvin

Reputation: 67

Get the line count from 2nd line of the file

How do I get the line count of a file from the 2nd line of the file, as the first line is header?

wc -l filename

Is there a way to set some condition into it?

Upvotes: 3

Views: 12086

Answers (6)

Cyrus
Cyrus

Reputation: 88939

Delete first line with GNU sed:

sed '1d' file | wc -l

Upvotes: 2

Rakib
Rakib

Reputation: 2096

Please try this one. It will be solved your problem

 $ tail -n +2 filename | wc -l 

Upvotes: 0

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21502

There is no way to tweak the wc command itself. You should whether process the result of the command, or use another tool.

As suggested in other answers, if you are running Bash, a good way is to put the result of the command into an arithmetic expression like $(( $(command) - 1 )).

In case if you are searching for a portable solution, here is a Perl version:

perl -e '1 while <>; print $. - 1' < file

The variable $. holds the number of lines read since a file handle was last closed. The while loop reads all the lines from the file.

Upvotes: 1

BraveNewCurrency
BraveNewCurrency

Reputation: 13065

Alternately, you could just subtract 2.

echo $((`cat FILE | wc -l`-2))

Upvotes: 0

anubhava
anubhava

Reputation: 786091

You can use awk to count from 2nd line onwards:

awk 'NR>1{c++} END {print c}' file

Or simply use NR variable in the END block:

awk 'END {print NR-1}' file

Alternatively using BASH arithmetic subtract 1 from wc output:

echo $(( $(wc -l < file) -1 ))

Upvotes: 3

hek2mgl
hek2mgl

Reputation: 158230

Use the tail command:

tail -n +2 file | wc -l

-n +2 would print the file starting from line 2

Upvotes: 5

Related Questions