I have a list of numbers extracted and need to delete the leading zeros from them, but when i do so, the command I am using also deletes numbers that end in Zero as well. eg 10, 20, 30, etc
this is part of a larger script and the only way I can think of is to try and detect the 10,20 30 etc in the first part of a if statement then deal with the remainder but that i think is quite messy as there may be many of them
Ideas?
My input to check is like this file below and trying to process with this code snipopet
file="leadingzero.txt" # input filename
while read line
do
wardevent=${line##*[!1-9]} # remove leading Zero from the line
echo $wardevent
done <"$file"
Sorry for that and thanks
Yes i would like the output without the leading zeros as per this
$ awk '{$1+=0}1' file
Is there a way to do it using string processing rather than calling awk?
Reason being as the section of the file is only a selection from a much larger line that is processed as part of a much larger string. The rest of the code pulls out the numbers and then I need to stip the leading zeros before putting them in an array.
I only included the extracted part to avoid confusion with the rest of the code.
In case you are interested in a much more efficient string processing approach which does not need to create an entirely new shell just to strip the zeroes, here's a modified version of pamu's suggestion:
while
read line
do
wardevent=${line#"${line%%[!0]*}"}
echo $wardevent
done
That assumes (like pamu's suggestion) that the value of $line consists of nothing but digits. If that is not the case, then the read statement needs to be fortified and the echo statement should be abandoned in favor of printf and $wardevent double-quoted.
Like the shell built-in numerics, printf "%d" treats a value with a leading 0 as an octal value, so 08 and 09 give error.
A workaround is printf "%.f" .
printf is a poor approach. Post #3 states that the result of stripping the leading zeroes will be saved for further use. To save printf's output, it's necessary to use command substitution, which does not improve upon the already accepted solution, $(expr ...) .
If the shell (or shells) which must run this code support the base# notation, I recommend MadeInGermany's arithmetic expansion approach, $((10#$v)) . It's efficient and so simple that it's immediately understandable.
If the code must run on a shell which does not support base# , then my suggestion is probably the best choice.
@alister, it would improve somewhat upon the accepted solution, since expr is an external command, whereas printf is not, although both would be slower than your suggested variable expansion of course.
Also $((10#$v)) is not available in every POSIX compliant shell like you stated
An advantage of the printf approach is that it would also handle cases like floating point and scientific notation and/or a leading plus signs..
Didn't I answer the question in post #4 in a way that others have replicated with more complex code?
I've written it as ksh, but the same works for bash
Not sure what all the worry is about.
For a faster process not reading line by line & looping, how about:-
sed s/^0*//g file
Does that fit the bill? It works for me.
I'd like to learn what I missed earlier and see if this is a better fit. :rolleyes:
So have I missed the point?
My reading of part of post #3:-
This suggested to me that there was more data on the line, not that the file was just a single column of numbers, which is why I gave a solution for a file like this:-
0001
0002 123
0003 Hello
Ho hum, I suppose that it will work for either. Do we have a happy questioner? That's the important bit I suppose. :rolleyes:
.... and hey, I've learnt a few techniques too, so I'm happy.
Hi robin we do have a happy Questioner
Lots of great ideas and solutions in here.
the reason I did not want to run either sed or awk is that the number in question is clipped out of a much longer line ( as a string). this has to repeated for many lines in the original files. Shelling out to awk / sed will make the loop processing to complicated where doing as part of a string is relatively easy within the loop.
Also this is a bash script but for the original question that was not important, but certainly interesting to read the differences in the responses.