0

INPUT

New,9,1
Old,11,0

OUTPUT

New,09,1
Old,11,0

I would like to append to filed number 2 after first , in case if it's single digit so i would like to add 0 before the digit as the output shown.

I were able to achieve that using awk but i lost , as separator.

using the following command

awk -F ',' '{if(length($2)==1)$2="0"$2;print $0}' inputfile
1
  • a small change in your awk command will do the needful awk -F ',' 'BEGIN { OFS=","};{if(length($2)==1)$2="0"$2;print $0}' Using OFS will ensure that the commas are printed
    – amisax
    Jan 8, 2019 at 7:16

3 Answers 3

0
awk -F ',' 'BEGIN { OFS=","};{if(length($2)==1)$2="0"$2;print $0}'
0

With GNU sed it is doable as like this :

   sed -Ee    's/,([0-9]),/,0\1,/'   input 
0

I have used below awk method to achieve the same

_linux_example ~]# awk -F "," '{if(length($2)<=1)print $1",0"$2","$3;else print $0}' file.txt

output:

 awk -F "," '{if(length($2)<=1)print $1",0"$2","$3;else print $0}' filename

New,09,1
Old,11,0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .