Kalin Borisov
Kalin Borisov

Reputation: 1120

Count how many rows are in one file with defined length to show

I want to count how many rows are in one file with defined length to show.

In normal case example:

file:

2423
546
74868

cat file|wc -l will show the result: 3.

But I want to defined length of the numbers for example to have 10 symbols and to show:

0000000003

if there are 100 lines to be:

0000000100

Upvotes: 0

Views: 99

Answers (3)

user3620917
user3620917

Reputation:

With awk:

awk 'END{printf("%010d\n",NR)}' file

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74685

I think that you want something like this:

printf '%010d' $(wc -l < file)

The format specifier %010d means print 10 digits, padded with leading 0s.

As a bonus, in using < to pass the contents of the file via standard input, I have saved you a "useless use of cat" :)

If I understand your comment correctly, you can do something like this:

printf 'Count: %010d' $(( $(wc -l < file) + 1 ))

Here I have added some additional text to the format specifier and used an arithmetic context $(( )) to add one to the value of the line count.

Upvotes: 4

mpapec
mpapec

Reputation: 50657

perl -ne '}{ printf("%010d\n", $.)' file

Reference to counting lines in perl.

Upvotes: 4

Related Questions