Remove Character From End Of Lines

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MusoCity
Posts: 95
Joined: 24 Mar 2018, 20:45

Remove Character From End Of Lines

27 Feb 2022, 04:26

I need to remove all | that are at the end of a line however many lines there are from a string:

[SSSSSS]
|test1|test2|test1|test2|test1|test2|
|test7|test7|test9||test7|test7|test9|
[EEEEE}

result
[SSSSSS]
|test1|test2|test1|test2|test1|test2
|test7|test7|test9||test7|test7|test9
[EEEEE}
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Remove Character From End Of Lines

27 Feb 2022, 05:33

Code: Select all

str := "
(
[SSSSSS]
|test1|test2|test1|test2|test1|test2|
|test7|test7|test9||test7|test7|test9|
)"

For key, val in StrSplit(str,"`n")
	res .= RTrim(val,"|") "`n"
MsgBox % res
HTH
MusoCity
Posts: 95
Joined: 24 Mar 2018, 20:45

Re: Remove Character From End Of Lines

27 Feb 2022, 07:18

It works with you example buts it's not working when I read the text file String

FileRead, String, %Filename%
Hardcoder
Posts: 278
Joined: 19 Feb 2022, 13:13
Location: Germany

Re: Remove Character From End Of Lines

27 Feb 2022, 07:27

Does your Textfile have trailing whitespaces?
Or line endings don't match?
sofista
Posts: 654
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Remove Character From End Of Lines

27 Feb 2022, 07:31

Try this RegEx approach, it works on example data:

Code: Select all

SampleText := "
(
[SSSSSS]
|test1|test2|test1|test2|test1|test2|
|test7|test7|test9||test7|test7|test9|
[EEEEE}
)"

MsgBox, % RegExReplace(SampleText, "m`a)\|$")

/* Output:

[SSSSSS]
|test1|test2|test1|test2|test1|test2
|test7|test7|test9||test7|test7|test9
[EEEEE}

 */
MusoCity
Posts: 95
Joined: 24 Mar 2018, 20:45

Re: Remove Character From End Of Lines

27 Feb 2022, 08:08

sofista wrote:
27 Feb 2022, 07:31
Try this RegEx approach, it works on example data:
Thanks, that worked. The last one worked with the text string in the script but not when copied to the text file.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Remove Character From End Of Lines

27 Feb 2022, 08:47

It works with you example buts it's not working when I read the text file String
FileRead, String, %Filename%
That's named GIGO. Not knowing that you've to rename the var from 'String' to 'str' so it'll work, is like blaming the soup while using a fork :lolno: :wtf:
And yes, you should attach your sample input file to this thread, so your supporters won't have to guess constantly. Thx for listening :|
BTW, what's the reason that this [EEEEE} is using a trailing curly brace, while [SSSSSS] isn't?
MusoCity
Posts: 95
Joined: 24 Mar 2018, 20:45

Re: Remove Character From End Of Lines

27 Feb 2022, 16:58

So I can't have

Code: Select all

For key, val in StrSplit(String,"`n")
	res .= RTrim(val,"|") "`n"
MsgBox % res
I have to have ?

Code: Select all

For key, val in StrSplit(str,"`n")
	res .= RTrim(val,"|") "`n"
MsgBox % res

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 383 guests