Remove only the last blank line from clipboard Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Remove only the last blank line from clipboard

27 Oct 2023, 10:04

Hi, I'm am having some problems removing the last blank line from the clipboard.
See below example, the clipboard will hold a text like this. How do I get rid of the last blank line in this text ?

I tried playing with SubStr, StrReplace, and Rtrim but I cannot seem to get it working.
Any pointers ? Thanks in advance.

Example of clipboard content:

Code: Select all

ClipboardContent :=  "
(
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	446333	Fake Article 1	1,00	44,54	0,00	EUR	1,2000	5772711	4820.10 5000	China	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	435112	Fake Article 2	1,00	44,54	0,00	EUR	0,8000	5772711	4820.10 5000	China	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	P	Packing and delivery	1,00	5,50	0,00	EUR	0,1000	5772711			

)"
User avatar
mikeyww
Posts: 27215
Joined: 09 Sep 2014, 18:38

Re: Remove only the last blank line from clipboard

27 Oct 2023, 11:29

Code: Select all

#Requires AutoHotkey v2.0

str := '
(
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	446333	Fake Article 1	1,00	44,54	0,00	EUR	1,2000	5772711	4820.10 5000	China	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	435112	Fake Article 2	1,00	44,54	0,00	EUR	0,8000	5772711	4820.10 5000	China	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	P	Packing and delivery	1,00	5,50	0,00	EUR	0,1000	5772711			


)'
If RegExMatch(str, 's)(.*)\R$', &m)
 str := m[1]
MsgBox '#' str '#'
vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: Remove only the last blank line from clipboard

27 Oct 2023, 11:31

@WalkerOfTheDay ClipboardContent is a variable. With assigned string value. Not a Clipboard.
You can try to skip empty lines inside parsing Loop, like as:

Code: Select all

Loop Parse ClipboardContent, '`n'
{
  If ( A_Loopfield != '')
  {
    ... does your things
  }
  Else
    continue
}
Please post your script code inside [code] ... [/code] block. Thank you.
User avatar
flyingDman
Posts: 2837
Joined: 29 Sep 2013, 19:01

Re: Remove only the last blank line from clipboard  Topic is solved

27 Oct 2023, 11:45

Rtrim should have worked, Try this:

Code: Select all

msgbox % ">>" rtrim(ClipboardContent," `n`r`t") "<<"
Deletes spaces, tabs, line feeds and carriage returns from the end. ">>" and "<<" helps when troubleshooting (remove when done).
14.3 & 1.3.7
User avatar
mikeyww
Posts: 27215
Joined: 09 Sep 2014, 18:38

Re: Remove only the last blank line from clipboard

27 Oct 2023, 12:10

Although three scripts have been posted, I believe that none of them match the description, which is to delete the single blank line in the string that is followed by no additional blank lines in the string. I took a guess in thinking that the need was actually to delete the final individual line in the string if that line was blank.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Remove only the last blank line from clipboard

27 Oct 2023, 12:33

mikeyww wrote:
27 Oct 2023, 12:10
Although three scripts have been posted, I believe that none of them match the description, which is to delete the single blank line in the string that is followed by no additional blank lines in the string. I took a guess in thinking that the need was actually to delete the final individual line in the string if that line was blank.
Indeed that's what I needed. My teenage daughter is hogging the pc, so I haven't time to test the code yet.

@vmech I'm aware that it wasn't a clipboard. It was merely to show what the data would look like.
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Remove only the last blank line from clipboard

27 Oct 2023, 15:01

Code: Select all

str:=RegExReplace(str,"sD)(.*?)\R+$","${1}")
 Yes!
If you want to remove just one line, you can use \R instead of \R+.
If you want to remove up to three lines, try \R{1,3}.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
str:="
(Join`r`n LTrim0 RTrim0
Interne ID document	Klant-/leveranciersnaam	Provincie	Gebouw	Straat	Postcode	Plaats	Land	Artikelnummer	Naam in vreemde taal	Hoeveelheid	Regeltotaal EUR	Regeltotaal (VV)	Prijsvaluta	Gewicht 1	Documentnummer	HS-Number	Land van herkomst	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	446333	Fake Article 1	1,00	44,54	0,00	EUR	1,2000	5772711	4820.10 5000	China	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	435112	Fake Article 2	1,00	44,54	0,00	EUR	0,8000	5772711	4820.10 5000	China	
953404	Michael Suhr			Fake Street 18 A	83550	FakeCity	FakeCountry	P	Packing and delivery	1,00	5,50	0,00	EUR	0,1000	5772711			



)"
str:=RegExReplace(str,"sD)(.*?)\R+$","${1}")
MsgBox A_Clipboard:=str
Last edited by Seven0528 on 27 Oct 2023, 15:28, edited 4 times in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Remove only the last blank line from clipboard

27 Oct 2023, 15:09

Code: Select all

str:=RTrim(str,"`r`n")
 I ended up using RegExReplace by accident(?), but in reality, RTrim is sufficient.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Remove only the last blank line from clipboard

30 Oct 2023, 04:05

So I did some testing, and both the RegExReplace versions as the Rtrim versions seem to be doing the job.
I'm going for the Rtrim version because I really do not understand the regex version. I've looked at the documentation before,
but I'm finding it quite intimidating.

Thanks for your help guys

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 46 guests