Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting numbers to fixed width

Tags:

bash

printf

I have this output:

30.1.2003
3.3.2003
25.12.2003

I want to make print each value except the year in two digits (might have leading values). i.e

30.01.2003
03.03.2003
25.12.2003
like image 810
Kimlotte Avatar asked Apr 18 '26 18:04

Kimlotte


2 Answers

You can use printf:

echo 30.1.2003 | tr . ' ' | xargs printf '%02d.%02d.%04d\n'
like image 78
John Zwinck Avatar answered Apr 20 '26 12:04

John Zwinck


awk -F. -v OFS="."  '{for(i=1;i<=NF;i++)$i=(length($i)<2?"0":"")$i}7' file

if your output was from some process, do :

yourApp|awk -F. -v OFS="."  '{for(i=1;i<=NF;i++)$i=(length($i)<2?"0":"")$i}7'
like image 32
Kent Avatar answered Apr 20 '26 12:04

Kent