2

I have several datalines like so:

v1.4.00.29                                                           
- SP.CNG v1.0.2.2                                                           
Update Kit - Secure USB Token v1.1.1.1                                                           
- HI_3997 v1.0.3997.1                                                           
- HI_4009 v1.0.4009.1                                                           
- HI_3585 v1.0.3585                                                           
Update Kit - RM4 v1.0.1202.4                                                           
Update Kit - DN Series v1.0.4.1   

Is there some easy way to check if the first character is a - and then delete this PLUS the space next to them so that the line is aligned to the other lines.

My first try was just to delete the -and spacesresulting in a not looking result as ALL - are replaced:

set tmp=!tmp:-=!
set tmp=!tmp: =!
Compo
  • 36,585
  • 5
  • 27
  • 39
SRel
  • 383
  • 1
  • 11
  • why not simply remove "dash+space" (`set tmp=!tmp:- =!`) instead of removing every dash and every space? – Stephan Dec 18 '19 at 11:23
  • He then also removes i there is a DASh and SPACE at different positions – SRel Dec 18 '19 at 11:25
  • ...which may or may not be a good idea. If not, TripeHound's solution is the best to remove them only at the beginning. (I just wondered why you removed both chars independently instead of their combination) – Stephan Dec 18 '19 at 11:30
  • To align with other lines, I would suggest it may be better to prepend eleven spaces to any lines beginning with `-`! – Compo Dec 18 '19 at 11:36

2 Answers2

3

You can do this using the "substring" method of referring to environment variables. In brief:

%TMP:~n,m%

will extract m characters from TMP, starting with the nth, where counting starts from zero. (You can also omit ,m to get "the rest of the string" and use negative numbers to mean "from the end of the string" – see the output of SET /? for more details).

In your case, something like the following should work:

if "%TMP:~0,2%" == "- "     set "TMP=%TMP:~2%"

This checks whether the first two characters are a minus and a space (you could relax this to just checking the first character if needed). If there's a match, it replaces TMP with all characters starting with the third (0=1st, 1=2nd etc.).

TripeHound
  • 2,721
  • 23
  • 37
  • @SRel While they can sometimes be fiddly, the substring commands can be quite powerful. For instance, with `SET Y=4` and `SET STR=abcdefgh`, then `echo !STR:~%Y%,1!` will extract the 5th letter (`e`). (Providing `setlocal enabledelayedexpansion` is in effect). – TripeHound Dec 18 '19 at 11:30
0

You might want to try the following code that uses sub-string substitution (the string to process is stored in variale tmp):

if "- !tmp:*- !"=="!tmp!" set "tmp=!tmp:*- =!"

This removes everything up to and including the first occurrence of - + SPACE from the string and precedes the result with - + SPACE; if this is now equal to the original string then the first - + SPACE must be at the beginning, so remove it; otherwise do not alter the string.

aschipfl
  • 33,626
  • 12
  • 54
  • 99