Pipe seq to printf for formatting numbers

I am trying to print the following template using printf and seq:

0000 0001 0002 0003 

My problem is that I use:

 seq 0 10 | xargs printf %04d 

all my output is formatted on the same line:

 0000000100020003 

I still can't figure out how to use xargs. How to use it in this case?

+6
source share
3 answers

The printf command does not output line breaks unless you request it. Try:

 seq 0 10 | xargs printf '%04d\n' 

Please note that you can achieve the same as using seq , since it allows you to specify the format in printf format:

 seq -f %04g 0 10 
+15
source

you do not need printf or xargs . seq has the -f option:

 kent$ seq -f '%04G' 10 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 
+7
source
 seq 0 10 | xargs printf "%04d\n" 

In the original question, there is no newline at the end of printf . Just adding a newline character fixes the problem.

0
source

Source: https://habr.com/ru/post/955155/


All Articles