remove non significative 0 in number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

remove non significative 0 in number

#1 Post by darioit » 23 Sep 2010 09:27

Hello everybody,

it's possible to do this in another way or with awk

the result must be a number without first non significative number0

Example row = "qwertyabc 000123"
I want extract with position start %rec2% and end %rec3% get the number 123 without initial 0

this is my code

Code: Select all

@echo off
set line1="qwertyabc 000123"
set Rec2=11
set Rec3=6

CALL SET rec_tot=1%%line1:~%Rec2%,%Rec3%%%
SET /A rec_tot=2*rec_tot-2%rec_tot:~1%
echo %rec_tot%

pause


Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: remove non significative 0 in number

#2 Post by aGerman » 23 Sep 2010 15:40

What about a FOR loop

Code: Select all

@echo off &setlocal enabledelayedexpansion
set line1="qwertyabc 000123"
set Rec2=11
set Rec3=6

for /f "delims=0 tokens=*" %%a in ("!line1:~%Rec2%,%Rec3%!") do echo %%a

pause

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: remove non significative 0 in number

#3 Post by ghostmachine4 » 23 Sep 2010 18:36

when you add you 0 to the field of numbers, awk "converts" it to integer "automagically"

Code: Select all

C:\test>echo qwertyabc 000123| gawk "{for(i=1;i<=NF;i++) if($i ~ /^[0-9]*$/) $i=$i+0}{print}"
qwertyabc 123


the above says, go through every field, check for digits, then add 0 to that field.
If you want to continue using this useful tool please read up on it here.. With adequate knowledge, it can replace cmd.exe as your lightweight programming tool.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: remove non significative 0 in number

#4 Post by ghostmachine4 » 23 Sep 2010 18:38

aGerman wrote:What about a FOR loop

Code: Select all

@echo off &setlocal enabledelayedexpansion
set line1="qwertyabc 000123"
set Rec2=11
set Rec3=6

for /f "delims=0 tokens=*" %%a in ("!line1:~%Rec2%,%Rec3%!") do echo %%a

pause

this is not a generic solution. What if the digits are else where and not at the specified positions?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: remove non significative 0 in number

#5 Post by orange_batch » 23 Sep 2010 19:58

Ouch guys, I know you can do better than that!

Code: Select all

@echo off&setlocal enabledelayedexpansion
set "line1=qwertyabc 000123"

for /f "tokens=1*" %%x in ("!line1!") do (
for /f "delims=0 tokens=*" %%z in ("%%y") do set numbers=%%z
)

echo:%numbers%


I changed it so line1 doesn't surround qwertyabc 000123 with quotes.

The first for splits the value of line1 where the space is.

The second for processes the second token of line1 (the numbers), removes any leading 0s and sets the value to numbers.

This can be useful when doing DOS math with numbers that have leading 0s, because DOS interprets any numbers with leading 0s as octal instead of decimal.

The for block can be easily shortened to a single line if you want...

Code: Select all

for /f "tokens=1*" %%x in ("!line1!") do for /f "delims=0 tokens=*" %%z in ("%%y") do set numbers=%%z

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: remove non significative 0 in number

#6 Post by ghostmachine4 » 23 Sep 2010 20:54

@orange, interesting..but there are flaws.

Code: Select all

C:\test>more test.bat
@echo off&setlocal enabledelayedexpansion
set "line1=qwertyabc 123xx 000123"

for /f "tokens=1*" %%x in ("!line1!") do (
for /f "delims=0 tokens=*" %%z in ("%%y") do set numbers=%%z
)

echo:%numbers%

C:\test>test.bat
123xx 000123


123xx is not a string of digits so it shouldn't be output... and 000123 is not converted.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: remove non significative 0 in number

#7 Post by amel27 » 24 Sep 2010 01:34

darioit wrote:extract with position start %rec2% and end %rec3% get the number 123 without initial 0
%rec3% is not required

Code: Select all

@echo off
SetLocal EnableDelayedExpansion

set "line=qwer yabc 000123 xxx"
set "skip=10"

for /f "tokens=* delims=0" %%i in ("!line:~%skip%!") do for /f %%a in ("%%i") do echo %%a


Code: Select all

@set "line=qwer yabc 000123 xxx"
@set "skip=10"
@set line|awk --re-interval "{f=gensub(/^[^=]*=.{%skip%}0*([0-9]+).*$/,\"\\1\",1);print f}"

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: remove non significative 0 in number

#8 Post by ghostmachine4 » 24 Sep 2010 03:58

amel27 wrote:

Code: Select all

@set "line=qwer yabc 000123 xxx"
@set "skip=10"
@set line|awk --re-interval "{f=gensub(/^[^=]*=.{%skip%}0*([0-9]+).*$/,\"\\1\",1);print f}"


there is no requirement to search for the "=" sign. There may not be equal signs in the string itself. OP just wanted any 000{digits} to be converted to remove leading 0's. And your awk reg expression capture only substitute 1 set of 000{digits}. you could use "g" instead to replace all.

Code: Select all

C:\test>echo qwer yabc 000123 0000123 0123xx |gawk --re-interval "{f=gensub(/\y0+([0-9]+)\y/,\"\\1\",\"g\");print f}"
qwer yabc 123 123 0123xx

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: remove non significative 0 in number

#9 Post by aGerman » 24 Sep 2010 05:02

ghostmachine4 wrote:
aGerman wrote:What about a FOR loop

Code: Select all

@echo off &setlocal enabledelayedexpansion
set line1="qwertyabc 000123"
set Rec2=11
set Rec3=6

for /f "delims=0 tokens=*" %%a in ("!line1:~%Rec2%,%Rec3%!") do echo %%a

pause

this is not a generic solution. What if the digits are else where and not at the specified positions?


Required was:
darioit wrote:I want extract with position start %rec2% and end %rec3% get the number 123 without initial 0


Regards
aGerman

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: remove non significative 0 in number

#10 Post by orange_batch » 24 Sep 2010 06:35

ghostmachine4 wrote:@orange, interesting..but there are flaws.

Code: Select all

C:\test>more test.bat
@echo off&setlocal enabledelayedexpansion
set "line1=qwertyabc 123xx 000123"

for /f "tokens=1*" %%x in ("!line1!") do (
for /f "delims=0 tokens=*" %%z in ("%%y") do set numbers=%%z
)

echo:%numbers%

C:\test>test.bat
123xx 000123


123xx is not a string of digits so it shouldn't be output... and 000123 is not converted.


I wrote it to handle a string in the format provided by the OP, so obviously more spaces would cause a failure. It doesn't need * as the second for's token either, if it's just a string of numbers with no spaces, but it shouldn't matter.

To be precise, it removes leading zeros from the second value of a space/tab delimited string and sets everything that follows.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: remove non significative 0 in number

#11 Post by amel27 » 24 Sep 2010 06:38

ghostmachine4 wrote:there is no requirement to search for the "=" sign.
type:

Code: Select all

@set "line=qwer yabc 000123 xxx"
@set line
result:
line=qwer yabc 000123 xxx


ghostmachine4 wrote:your awk reg expression capture only substitute 1 set of 000{digits}. you could use "g" instead to replace all
regular expression like "^...$" has only 1 entry per line... ;)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: remove non significative 0 in number

#12 Post by ghostmachine4 » 24 Sep 2010 07:20

@aGerman, amel27, i don't know about you, but i don't assume that OP wants to really hard code that one piece of string in his batch. I would assume that lines come from somewhere else, like a file or some standard output where positions aren't fixed and that there are other line formats with one or more 0*{digits}.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: remove non significative 0 in number

#13 Post by ghostmachine4 » 24 Sep 2010 07:24

amel27 wrote: expression like "^...$" has only 1 entry per line... ;)

that i know. your code may work if that's the only type of line OP has and has ONLY one "000123".

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: remove non significative 0 in number

#14 Post by amel27 » 24 Sep 2010 08:45

ghostmachine4 wrote:your code may work if that's the only type of line OP has and has ONLY one "000123".

yes!.. every line has ONLY one fixed position %rec2% :)

P.S. it is possible too - some tools formatting output fields by position, not by delimiters

Post Reply