Extracting a specified line from a textfile

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Billykid
Posts: 98
Joined: 16 Sep 2019, 08:59

Extracting a specified line from a textfile

25 Mar 2024, 12:24

Code: Select all

line := 3 ; Line number to extract
; String from textfile:
str := "Line_1`r`nLine_2`r`nLine_3`r`nLine_4`r`nLine_5`r`nLine_6`r`nLine_7`r`nLine_8`r`nLine_9"
; or
str := "Line_1`nLine_2`nLine_3`nLine_4`nLine_5`nLine_6`nLine_7`nLine_8`nLine_9"

Is it possible to extract a specified line by a given string using a regular expression?
A big thank you for your efforts in advance.
User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: Extracting a specified line from a textfile

25 Mar 2024, 12:38

Why not use FileReadLine and read only the line you want directly from the file?
User avatar
ositoMalvado
Posts: 183
Joined: 24 Dec 2019, 12:02
Contact:

Re: Extracting a specified line from a textfile

25 Mar 2024, 12:57

Maybe it can be better, but it works xd

Code: Select all

example1 := "red`ngreen`nblue"
MsgBox, % getLine(example1, 2)

example2 := "red`r`ngreen`r`nblue"
MsgBox, % getLine(example2, 3)

getLine(text, line:=1, delim:="`n"){
    Loop, parse, % text, % delim
    {
        If (A_Index == line)
            Return A_LoopField
    }
    Return "Can't find, check your parameters"
}
My WEB some useful stuff
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: Extracting a specified line from a textfile

25 Mar 2024, 12:59

Another one is below.

Code: Select all

; This script gets a line from a text string
#Requires AutoHotkey v1.1.33.11
lineNum := 3 ; Line number to get
str     := "Line_1`r`nLine_2`r`nLine_3`r`nLine_4`r`nLine_5`r`nLine_6`r`nLine_7`r`nLine_8`r`nLine_9"
str     := "Line_1`nLine_2`nLine_3`nLine_4`nLine_5`nLine_6`nLine_7`nLine_8`nLine_9"
MsgBox 64, % "Line " lineNum, % getLine(str, lineNum)

getLine(str, lineNum) {
 Return StrSplit(str, "`n", "`r")[lineNum]
}
If you are working with a text file, then boiler's approach is easy in a single line.

Code: Select all

; This script gets a line from a file
#Requires AutoHotkey v1.1.33.11
filePath := "test.txt"
lineNum  := 3 ; Line number to get
FileReadLine line, % filePath, lineNum
MsgBox 64, % "Line " lineNum, % line
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Extracting a specified line from a textfile

26 Mar 2024, 02:52

Hallo,
for a text file the above is preferable!
Otherwise:

Code: Select all

line := 3 ; Line number to extract
; Line from text:
str := "Line_1`r`nLine_2`r`nLine_3`r`nLine_4`r`nLine_5`r`nLine_6`r`nLine_7`r`nLine_8`r`nLine_9"
MsgBox,% RegExMatch(str, "^(.*?(\R|$)){" line "}", M)?M1:""
str := "Line_1`nLine_2`nLine_3`nLine_4`nLine_5`nLine_6`nLine_7`nLine_8`nLine_9"
MsgBox,% RegExMatch(str, "^(.*?(\R|$)){" line "}", M)?M1:""
Billykid
Posts: 98
Joined: 16 Sep 2019, 08:59

Re: Extracting a specified line from a textfile

26 Mar 2024, 07:39

Thank you very much for your great help and the many alternatives you have shown me. As always, you have helped me a lot.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: just me, MiM and 146 guests