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

109 views Asked by At

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
3

There are 3 answers

2
Tom Fenech On BEST ANSWER

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.

3
AudioBubble On

With awk:

awk 'END{printf("%010d\n",NR)}' file
0
mpapec On
perl -ne '}{ printf("%010d\n", $.)' file

Reference to counting lines in perl.