Search code examples
bashperlawkwc

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

Solution

  • 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.