Changing last entries in a comma delimited list
awk approach with sprintf function(to add leading zeros):
awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file
The output:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
-F,
- set comma ,
as field separator
$8
- points to the eighth field
%02d
- format which treats function argument as 2-digit number
Note, the last field in a record can be presented by $NF
.
NF is a predefined variable whose value is the number of fields in the current record
So, $NF
is the same as $8
(for your input)
awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file
You can try using awk
:
awk 'BEGIN { FS = OFS = "," } { $NF = sprintf("MI-%02d", $NF); } 1' file
Here's perl solution:
$ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
The -a
flag allows us to treat input as array, based on separator specified with -F
. Basically we alter last item in that array, and rebuild it via join
command.