How to delete space at satrting of line...

General Macro Scheduler discussion

Moderators: JRL, Dorian (MJT support)

Post Reply
JVM
Junior Coder
Posts: 40
Joined: Wed Oct 10, 2007 1:14 pm
Location: delhi (india)
Contact:

How to delete space at satrting of line...

Post by JVM » Thu Jan 17, 2008 4:12 am

Hi friend ,

I am facing a problem how can i remove the space at the starting of line.

for example :-

abcdefghijklmn


is there any function for this so i can remove all the space (if present in the text file )


thanks a lot
hi every one.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Thu Jan 17, 2008 5:47 am

Hi JVM,

One way is to use Regular Expressions (regex) and VBScript. I found this example from Marcus here:

Remove multiple spaces from text
http://www.mjtnet.com/usergroup/viewtopic.php?t=2009

All it needed was a slight modification to add ^ at the start of the regex pattern to tell it to only match at the beginning of the string... so the modified example below will match one or more "whitespace characters" (a space is one of those but there are others like Tab, etc.) but only at the start of the line... multiple "whitespace characters" within or at the end of the line will not be matched.

Hmm... that would match leading Tabs too. Since you only mentioned spaces, I further changed the regular expression pattern in the example below from "^\s+" to "^ +" which means, match one or more spaces at the beginning of the line. Note that the " chars are not part of the regex pattern itself, they're just delimiters.

Here's the modified code example, again, with thanks to Marcus for providing the original example:

Code: Select all

VBSTART
Function ReplWhiteSpace(strng,replchar)
   Dim regEx
   Set regEx = New RegExp
   regEx.Pattern = "^ +"
   regEx.Global = True
   ReplWhiteSpace = regEx.Replace(strng,replchar)
End Function
VBEND

Let>Line=       Hello.    How    are you    today?
MDL>Before:%Line%
VBEval>ReplWhiteSpace("%Line%",""),newval
MDL>After:%newval%
Note that what you put between the "" in the line below...
  • VBEval>ReplWhiteSpace("%Line%",""),newval
...is what you are replacing any leading space or spaces with. In this case, we have nothing between the " chars as that's what you want... to replace them with nothing.

The above example works for me, hope it works for you.

Enjoy and take care.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

JVM
Junior Coder
Posts: 40
Joined: Wed Oct 10, 2007 1:14 pm
Location: delhi (india)
Contact:

deletin space at the strting of the line..

Post by JVM » Thu Jan 17, 2008 6:31 am

Hi friend,

thanks for the reply but friend i want to use only macro script.. :(

is this possible i am waiting for reply

Please help me
hi every one.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Jan 17, 2008 8:32 am

To remove ALL spaces just do:

StringReplace>line, ,,line

To remove only leading spaces do:

VBSTART
VBEND

VBEval>LTrim("%line%"),line
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

JVM
Junior Coder
Posts: 40
Joined: Wed Oct 10, 2007 1:14 pm
Location: delhi (india)
Contact:

How would i ensure that there is no empty line ?

Post by JVM » Thu Jan 17, 2008 12:06 pm

well friends,

One more question


how would i ensure that the give lines last character is the last character of text file and there is no blank line ........


Example:-



i want to ensure that " > "is the last character and their is no further empty line.



waiting for reply... friend
hi every one.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Jan 17, 2008 3:10 pm

Are the ending "blank" lines really blank?

If they are just Return codes, then try replacing all %CRLF%%CRLF% with nothing.

But that will also remove any blank lines in the body of the text.
------------------------
Or, read the file, line by line, and rewrite to another file.
Do not write any second sequential Return code. That should end up with a single return code at the end.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

JVM
Junior Coder
Posts: 40
Joined: Wed Oct 10, 2007 1:14 pm
Location: delhi (india)
Contact:

how to ensure that the ">" is the last charcter

Post by JVM » Fri Jan 18, 2008 6:29 am

hello fiends ,


well as u said to do. i did exactly same but i have problem that i can not replace all %CRLF% from script .


Actually i am giving the data how it should come



hi this a testing data.


hi this a testing data.


hi this a testing data.


hi this a testing data.


Now what my objective is to ensure that there is no space after
and there is no empty line after this.....


if replace %CRLF% than all data will come in a line but want as it is given
so is this possible friend ..
help me out ..
eagerly waiting for reply...
hi every one.

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jan 18, 2008 1:35 pm

Set line one so that the variable file1 refers to your path and file name.

Code: Select all

Let>file1=C:\JVM.txt

ReadFile>%file1%,file
Separate>%file%,>,line
Sub>line_count,1
Let>counter=1
Concat>line_1,>
Repeat>counter
  Add>counter,1
  Let>value=line_%counter%
  Concat>line_1,%value%>
  Message>Reading line %counter% of %line_count%
Until>counter,%line_count%

Let>WLN_NOCRLF=1
WriteLn>%temp_dir%~newfile~.tmp,wres,%line_1%
Let>CF_OVERWRITE=1
CopyFile>%temp_dir%~newfile~.tmp,%file1%
DeleteFile>%temp_dir%~newfile~.tmp
MDL>Reading line %counter% of %line_count%%CRLF%Complete...%CRLF%%CRLF%Press OK to continue.

JVM
Junior Coder
Posts: 40
Joined: Wed Oct 10, 2007 1:14 pm
Location: delhi (india)
Contact:

deleting empty lines

Post by JVM » Tue Jan 22, 2008 11:10 am

well friends

still unresolved .......
i am having a problem i want to delete all empty lines which appears any where...


Now what i got after running the empty line script
for example :-
..........................................................................................................
this is a test data.
this is a test data.
this is a test data.
this is a test data.
this is a test data.
this is a test data.
this is a test data.Press down arrow
cursor position
..................................
if i put my cursor at the end of last line and than press down it goes to next line. it shows there is an empty line. which i don't want so how can i remove this ?

Used code

Code: Select all

GetClipBoard>xyz
Separate>%xyz%,CRLF,lines
Let>new1=
Let>k1=0
Repeat>k1
Let>k1=k1+1
Let>thisline=lines_%k1%
Length>thisline,result
if>result>2
ConCat>%new1%,%thisline%
ConCat>%new1%,%CRLF%
endif
Until>k1=lines_count
Let>remarks=new1
PutClipBoard>%remarks%




Please friend help me
eagerly waiting for reply

JVM
hi every one.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jan 22, 2008 12:34 pm

So all you need to do is truncate the entire data by two characters (CR and LF are two characters at the end and you want to remove them):

Code: Select all

//to remove last CRLF, simply truncate by 2 chars
GetClipBoard>data

Length>data,lenD
MidStr>data,1,{%lenD%-2},data

PutClipBoard>data
If you want to be sure that the last two chars are indeed CR and LF respectively first, do this:

Code: Select all

//to remove last CRLF, simply truncate by 2 chars
GetClipBoard>data

Length>data,lenD
//what are the last two chars?  Are they CRLF?
MidStr>data,{%lenD%-1},2,lastChars

//if last two chars are CRLF pair, remove them
If>lastChars=CRLF
  MidStr>data,1,{%lenD%-2},data
  PutClipBoard>data
Endif
This will only remove the last two chars if they are CRLF. Hopefully you can see how it would be easy to do the above in a recursive loop if you want to make it handle the case where there could be several blank lines in a row.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Thu Feb 14, 2008 4:04 am

mtettmar wrote:To remove only leading spaces do:

VBSTART
VBEND

VBEval>LTrim("%line%"),line
Hi Marcus,

This is similar, I'm using Trim instead of LTrim.

I am finding that embedded " characters in the string cause the VBScript Trim function to fail, try this:

Code: Select all

VBSTART
VBEND
Let>line=    Can lines with embedded " chars be trimmed using VBScript?       
VBEval>Trim("%line%"),line
There are both leading and trailing spaces in that string.

Is there a way around this problem that would make Trim able to handle " characters... and all other odd characters for that matter?

If not, do you have a suggested Macro Script solution for trimming both leading and trailing spaces?

Any other suggestions from the wizards at large also welcome... looking for something unbreakable...
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Feb 14, 2008 6:26 am

As quotes are string delimiters in vbscript they need to be double quoted. Do this first:

StringReplace>line,","",line

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Thu Feb 14, 2008 5:14 pm

That fixed it, thanks Marcus.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts