Skip to content
Advertisement

AWK script automatically removing leading 0s from String

I have a file BLACK.FUL.eg2:

JavaScript

I’ve written this AWK script:

JavaScript

which gives me an output of:

JavaScript

with one problem: the leading 0s of strings in field1, are automatically getting removed due to a numeric operation on them. So my actual expected output is:

JavaScript

For that I’m trying the below updated AWK script:

JavaScript

But getting an error:

JavaScript

Can you please highlight what I’m doing wrong to achieve the expected output. Modification only for my already existing AWK script will be of much help. Thanks

NOTE: The Leading 0s can be of any number of occcurence, not only 2 0s in every case as in the above example outputs.

Advertisement

Answer

since your field sizes are fixed, for the given example just change the last print statement to

JavaScript

UPDATE

if your field size is not fixed, you can capture the length (or desired length) and use the same pattern. Since your code is too complicated, I’m going to write a proof of concept which you can embed into your script.

this is essentially your problem, increment a zero padded number and the leading zeros dropped.

JavaScript

this is the proposed solution with parametric length with zero padding.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement