sed 例如:“1”到“01”-怎么做?

sed 例如:“1”到“01”-怎么做?

如果有人能写出更好的主题,那么我会很高兴:D

INPUT:
201103 1 /mnt/hdd/PUB/SOMETHING
201102 7 /mnt/hdd/PUB/SOMETH ING
201103 11 /mnt/hdd/PUB/SO METHING
201104 3 /mnt/hdd/PUB/SOMET HING
201106 1 /mnt/hdd/PUB/SOMETHI NG

I NEED THIS OUTPUT:
201103 01 /mnt/hdd/PUB/SOMETHING
201102 07 /mnt/hdd/PUB/SOMETH ING
201103 11 /mnt/hdd/PUB/SO METHING
201104 03 /mnt/hdd/PUB/SOMET HING
201106 01 /mnt/hdd/PUB/SOMETHI NG

如果“天”部分只有“1”,我该如何添加“0”?[我需要的日期格式是:YYYYMM DD]

答案1

您可以尝试例如这个:

sed -r 's/^([0-9]{6}\s)([0-9]\s)/\10\2/'

答案2

$ sed 's/\(\<[0-9]\>\)/0\1/' ./infile
201103 01 /mnt/hdd/PUB/SOMETHING
201102 07 /mnt/hdd/PUB/SOMETH ING
201103 11 /mnt/hdd/PUB/SO METHING
201104 03 /mnt/hdd/PUB/SOMET HING
201106 01 /mnt/hdd/PUB/SOMETHI NG

https://unix.stackexchange.com/questions/9137/sed-eg-1-to-01-how

相关内容