Striipping leading zeros from a field in awk

I need to strip some leading zeros in a field that I have extracted from a UNIX text file using awk.

There are two scenarios.

1. The field consistently has a single zero on the left of every row. Unless there is a better way I will use substr() to get rid of that

2. The field has a variable number of zeros padding it on the left. I need to strip away the zeros until I get to the first non-zero character then leave it at that. If I add zero to the field and print the result, will that work? or is there a better way?

For example:

1. 020090101 becomes 20090101
2. 00087887887 becomes 87887887
    00232200222 becomes 00232200222
    1118818100    stays as it is

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

awk '{sub(/^0+/,"")}1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stummjAuthor Commented:
This is within an awk script ozo, so what is the syntax to apply it to variable $7 (for example)

Also, if there are more than one leading zero in the first example, will it not screw things up?
Todd MummertCommented:

awk '{sub(/^0+/,"",$7)}'

your addition works as well

awk '{$7+=0}'

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

awk '{sub(/^0+/,"",$7)}1'
what sort of screw up would it do?
would not work if $7 was
why not try
awk '{printf "%d", $7}' <inputfile>
with linefeed -
awk '{printf "%d\n",  $7}' <inputfile>
stummjAuthor Commented:
Well in the first example, where I need to strip off just the first leading zero, if there happened to be 2 zeros at the start, wouldnt it remove both of them?

Thats what I need in the second example but not in the first
I assumed you would want to remove both of them.
Another question might be, what would you want to if it is nothing but zeros
Todd MummertCommented:

just to clarify your last question, this small change to ozo's would remove just the first 0

awk '{sub(/^0/,"",$7)}'
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.