Remove leading zeros from a text file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
GEOVAN
Posts: 233
Joined: 03 Mar 2022, 11:12

Remove leading zeros from a text file

Post by GEOVAN » 07 Dec 2024, 17:02

Hello,
I have a text file lets say "tfilename.txt"
In that text file there are written numbers as follow for example:
001
002
003
004
005
etc

I want to remove the leading zeros from ALL the lines, i try the following:

Code: Select all

FileRead, MyText, tfilename.txt
MyText := LTrim(MyText, "0")
FileAppend , %MyText% , tfilename2.txt
The above creates a new text file named tfilename2.txt, which removes the leading zero ONLY from the FIRST line as follows:
1
002
003
004
005
etc

But, how can i renove the leading zeros from ALL the lines, as follow, please: ?
1
2
3
4
5
etc

User avatar
flyingDman
Posts: 3072
Joined: 29 Sep 2013, 19:01

Re: Remove leading zeros from a text file

Post by flyingDman » 07 Dec 2024, 17:18

Use regexreplace(MyText,"0+(\d+)","$1")
14.3 & 1.3.7

GEOVAN
Posts: 233
Joined: 03 Mar 2022, 11:12

Re: Remove leading zeros from a text file

Post by GEOVAN » 08 Dec 2024, 02:02

flyingDman wrote:
07 Dec 2024, 17:18
Use regexreplace(MyText,"0+(\d+)","$1")
Thank you very much!!! It works perfect!

There is only one additional problem:
Is there a way to add a command in order to sort the list in NUMERICAL ORDER?

For example, let s say we have the following in the text file:
01
002
00030
000000011

By applying the regexreplace(MyText,"0+(\d+)","$1") , we get:
1
2
30
11

What command should we use in order to get also a NUMERICAL ORDER , so the desired result to be as the following, please ?
1
2
11
30

User avatar
JoeWinograd
Posts: 2256
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Remove leading zeros from a text file

Post by JoeWinograd » 08 Dec 2024, 04:23

Hi @GEOVAN,
I don't know if there's a way to do it inside the regexreplace (I'm sure flyingDman will let us know if there is), but a simple way to do it is to put Sort MyText,N either before or after MyText:=regexreplace(MyText,"0+(\d+)","$1") (I suggest reading the doc for Sort, which explains that the N option does a numeric sort). Regards, Joe

GEOVAN
Posts: 233
Joined: 03 Mar 2022, 11:12

Re: Remove leading zeros from a text file

Post by GEOVAN » 08 Dec 2024, 07:28

JoeWinograd wrote:
08 Dec 2024, 04:23
Hi @GEOVAN,
I don't know if there's a way to do it inside the regexreplace (I'm sure flyingDman will let us know if there is), but a simple way to do it is to put Sort MyText,N either before or after MyText:=regexreplace(MyText,"0+(\d+)","$1") (I suggest reading the doc for Sort, which explains that the N option does a numeric sort). Regards, Joe
THANK YOU VERY VERY MUCH!!! It works perfect!
I like the SORT command of ahk, and i try it to the following example:

For example, let s say we have the following in the text file:
L000000011
L00030
L002
L01


Very strange - By applying the Sort MyText,N , we get:
L00030
L002
L01
L000000011


Why this strange phenomenon, please?
How can we modify the wonderful command Sort MyText,N , in order to take into account the case that the number is after letters, please , so the desired result to be ALPHABETICAL and NUMERICAL ORDER at the same time as the following, please ? (Like WINDOWS EXPLORER SHOWING)
L01
L002
L000000011
L00030

User avatar
flyingDman
Posts: 3072
Joined: 29 Sep 2013, 19:01

Re: Remove leading zeros from a text file

Post by flyingDman » 08 Dec 2024, 11:04

Code: Select all

txt =
(
L00030
L000000011
L002
L01
)

Sort, txt, F logical_cmp
msgbox % txt

logical_cmp(a,b, o) {
	return dllcall("Shlwapi.dll\StrCmpLogicalW", "wstr", a, "wstr", b, "int")
}
In v2 this is built in (option Clogical)
14.3 & 1.3.7

GEOVAN
Posts: 233
Joined: 03 Mar 2022, 11:12

Re: Remove leading zeros from a text file

Post by GEOVAN » 08 Dec 2024, 11:11

flyingDman wrote:
08 Dec 2024, 11:04

Code: Select all

txt =
(
L00030
L000000011
L002
L01
)

Sort, txt, F logical_cmp
msgbox % txt

logical_cmp(a,b, o) {
	return dllcall("Shlwapi.dll\StrCmpLogicalW", "wstr", a, "wstr", b, "int")
}
In v2 this is built in (option Clogical)

THANK YOU very very much!!!
This is perfect!!!

User avatar
Chunjee
Posts: 1657
Joined: 18 Apr 2014, 19:05
Contact:

Re: Remove leading zeros from a text file

Post by Chunjee » 09 Dec 2024, 14:05

GEOVAN wrote:
08 Dec 2024, 07:28
For example, let s say we have the following in the text file:
L000000011
L00030
L002
L01

Just for fun:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

txt =
(
L00030
L000000011
L002
L01
)

interestingSort(strSplit(txt, "`n", "`r"))
; => ["L01", "L002", "L000000011", "L00030"]

interestingSort(inputVar) {
	arr := []
	for key, value in inputVar {
		value := {"originalValue": value, "numbersOnly": biga.replace(value, "/(\D+)/", "")}
		arr.push(value)
	}
	return biga.map(biga.sortBy(arr, "numbersOnly"), "originalValue")
}
return
The idea is simple. Remove anything that is not a number, perform the sort (numerical), return the original value.

https://biga-ahk.github.io/biga.ahk/#/?id=replace
https://biga-ahk.github.io/biga.ahk/#/?id=map
https://biga-ahk.github.io/biga.ahk/#/?id=sortby

Post Reply

Return to “Ask for Help (v1)”