I have a CSV formatted multi lines file with 5 columns (fields). I need to unify and corrected the corrupted first column which has lots of different formats of the code I need to unify. The complete final format of my code for the first column should be 00AB[0-9][0-9][0-9][0-9][0-9] which [0-9] could be any number such as 00AB21345.  The first four digits i.e. 00AB should be always as it is. but the 5 digits after that (i.e.[0-9][0-9][0-9][0-9][0-9]) could be any number and if there would  be any > 5 digits , the missing digits from far left should be substitute with 0. Example  <111> --> <00AB00111> ; or <1111> --> <00AB01111>. To have an example let's say I have a following file :111     xx  yy  zzz ddd1111    xx  yy  zzz ddd11111   xx  yy  zzz dddA111    xx  yy  zzz dddA1111   xx  yy  zzz dddA11111  xx  yy  zzz dddAB111   xx  yy  zzz dddAB1111  xx  yy  zzz dddAB11111 xx  yy  zzz ddd0A111   xx  yy  zzz ddd0A1111  xx  yy  zzz ddd0A11111 xx  yy  zzz ddd0AB111  xx  yy  zzz ddd0AB1111 xx  yy  zzz ddd0AB11111 xx yy  zzz ddd00A111  xx  yy  zzz ddd00A1111 xx  yy  zzz ddd00A11111xx  yy  zzz ddd00AB111 xx  yy  zzz ddd00AB1111 xx yy  zzz ddd0AB11111 xx yy  zzz ddd00AB12344   xx  yy  zzz ddd00AB34527   xx  yy  zzz ddd00AB56278   xx  yy  zzz ddd00AB98902   xx  yy  zzz dddTo cover all the possible scenario I made up the following very long awk script. The bold format represent the potential scenario could be find in my file which needed to be corrected.My request, dose any one know any awk script to address this in much smaller script? If so, would you explain it to me in details to learn :)##111 Awk -F',' '{if($0~/[0-9][0-9][0-9]/){print 001AB00suBstr($1,1,3),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' SC3.csv > y1.csv##1111Awk -F',' '{if($0~/[0-9][0-9][0-9][0-9]/){print 001ABsuBstr($1,1,4),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y1.csv > y2.csv##11111Awk -F',' '{if($0~/[0-9][0-9][0-9][0-9][0-9]/){print 001AB suBstr($1,1,5),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y2.csv > y3.csv##A111Awk -F',' '{if($0~/[A-Z][0-9][0-9][0-9]/){print 001suBstr($1,1,1) B00suBstr($1,2,4),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y3.csv > y4.csv##A1111Awk -F',' '{if($0~/[A-Z][0-9][0-9][0-9][0-9]/){print 001suBstr($1,1,1) B0 suBstr($1,2,5),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y4.csv > y5.csv##A11111Awk -F',' '{if($0~/[A-Z][0-9][0-9][0-9[0-9][0-9]/){print 001suBstr($1,1,1) B suBstr($1,2,6),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y5.csv > y6.csv##AB111Awk -F',' '{if($0~/[A-Z][A-Z][0-9][0-9][0-9]/){print 001suBstr($1,1,2) 00 suBstr($1,3,5),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y6.csv > y7.csv##AB1111Awk -F',' '{if($0~/[A-Z][A-Z][0-9][0-9][0-9][0-9]/){print 001suBstr($1,1,2)0 suBstr($1,3,6),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y7.csv > y8.csv##AB11111Awk -F',' '{if($0~/[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]/){print 001suBstr($1,1,7),$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y8.csv > y9.csv##1A111Awk -F',' '{if($0~/[0-9][A-Z][0-9][0-9][0-9]/){print 00suBstr($1,1,2) ,B00 suBstr($1,3,5) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y9.csv > y10.csv##1A1111  Awk -F',' '{if($0~/[0-9][A-Z][0-9][0-9][0-9][0-9]/){print 00suBstr($1,1,1) B0 suBstr($1,3,6) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y10.csv > y11.csv##1A11111Awk -F',' '{if($0~/[0-9][A-Z][0-9][0-9][0-9][0-9][0-9]/){print 00suBstr($1,1,2) B suBstr($1,3,7) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y11.csv > y12.csv##1AB111Awk -F',' '{if($0~/[0-9][A-Z][A-Z][0-9][0-9][0-9]/){print 00suBstr($1,1,1) suBstr($1,1,3)00 suBstr($1,4,6) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y12.csv > y13.csv##1AB1111Awk -F',' '{if($0~/[0-9][A-Z][A-Z][0-9][0-9][0-9][0-9]/){print 00 suBstr($1,1,3) 0 suBstr($1,4,7) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y13.csv > y14.csv##1AB11111Awk -F',' '{if($0~/[0-9][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]/){print 00 suBstr($1,1,8) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y14.csv > y15.csv##11A111Awk -F',' '{if($0~/[0-9][0-9][A-Z][0-9][0-9][0-9]/){print 0 suBstr($1,1,3)B00 suBstr($1,4,6) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y15.csv > y16.csv##11A1111Awk -F',' '{if($0~/[0-9][0-9][A-Z][0-9][0-9][0-9]/){print 0 suBstr($1,1,3)B0 suBstr($1,4,7) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y16.csv > y17.csv##11A11111Awk -F',' '{if($0~/[0-9][0-9][A-Z][0-9] [0-9][0-9][0-9]/){print 0 suBstr($1,1,3)B suBstr($1,4,8) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y17.csv > y18.csv##11AB111Awk -F',' '{if($0~/[0-9][0-9] [A-Z][[A-Z][0-9][0-9][0-9]/){print 0 suBstr($1,1,4)00 suBstr($1,5,7) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y18.csv > y19.csv##11AB1111Awk -F',' '{if($0~/[0-9][0-9] [A-Z][[A-Z][0-9][0-9][0-9][0-9]/){print 0 suBstr($1,1,4)0 suBstr($1,5,8) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y19.csv > y20.csv##1AB11111Awk -F',' '{if($0~/[0-9][0-9] [A-Z][[A-Z][0-9][0-9][0-9][0-9][0-9]/){print 0 suBstr($1,5,9) ,$2,$3,$4,$5;}else{print $1,$2,$3,$4,$5;}}' y20.csv > y21.csv` 
How to use awk to correct and unify a corrupted file with multiple columns and lines?
linux;shell script;command line;awk
Maybe:awk 'sub(^0?0?A?B?,,$1) && $1=sprintf(00AB%05d,$1)'Delete any leading 00AB fragments from field 1, then convert it to 00AB followed by the rest of the number padded with zeros up to length 5.The expression is always true so the implicit { print } action fires. The sub is always true because the regular expression is nullable: a bit sneaky! The substitution takes place even if ^0?0?A?B? matches the empty string, because that is a successful match.