If you just want to add a 0 if the first column is a single character, this will do:
sed -e 's/^.,/0&/' input.txt
In lines where the second character is a comma, it prepends a 0.
If the maximum length of your first column becomes 3 instead of 2, then you can do like this:
sed -e 's/^.,/00&/' -e 's/^..,/0&/' input.txt
Or, if you want to make this all dynamic, and pad as many zeros as necessary depending on the longest value in the first column, you could use this awk
:
awk -F, 'NR == 1; NR > 1 { data[NR] = $0; w1[NR] = length($1); if (length($1) > max) max = length($1) } END { for (i=2; i<= NR; ++i) { w = max - w1[i]; if (w > 0) printf "%0*d", w, 0; print data[i] } }' input.txt
The same thing but expanded to multiple lines for readability, with comments:
awk -F, '
NR == 1 # the first line is the header, just print it as it is
NR > 1 {
data[NR] = $0 # save the line
w1[NR] = length($1) # save the width of 1st field
if (length($1) > max) max = length($1) # update max length
}
END { # pass 2: now that we know max length, print the lines
for (i = 2; i <= NR; ++i) {
w = max - w1[i] # calculate the zeros we need to prepend
if (w > 0) printf "%0*d", w, 0 # print w zeros, if necessary
print data[i] # print the saved line
}
}' input.txt
s.no
are 4 characters. Aren't your fields defined by commas? Are we supposed to skip the first line as a header? How do we know the max length? Should we first read the entire file and find the longest field?